0

I have two classes to map a JSON response: Item and FrequentProps

Item has the following properties:

frequentProps, identifier, name

FrequentProps has the properties

propOne
propTwo
propThree
propFour

You can see that frequentProps in Item is of type FrequentProps.

Consider the following JSON Response:

[
    {
        "frequentProps": [
            {
                "propOne": 174
            },
            {
                "propTwo": 9.726
            },
            {
                "propThree": 2.021
            },
            {
                "propFour": 25.07
            }
        ],
        "identifier": "4223",
        "name": "TheName"
    }
]

The outer part of the JSON is supposed to be mapped to an object of class Item, the nested Array is supposed to be mapped to frequentProps, as a property of the object. Unfortunately, frequentProps is not mapped to the Items property with the same name but into an NSArray (if I define the type of the property as NSArray, otherwise the property remains nil).

Here's the configuration:

RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[Item class]];
[item addAttributeMappingsFromDictionary:[Item attributesMapping]];
RKObjectMapping *frequentPropsMapping = [RKObjectMapping mappingForClass:[FrequentProps class]];    
[frequentPropsMapping addAttributeMappingsFromDictionary:[FrequentProps attributesMapping]];
[itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"frequentProps"
                                                                                      toKeyPath:@"frequentProps"
                                                                                    withMapping:frequentProps]];

// adding the response descriptor, etc...

How can I map the frequentProps directly into an object of type FrequentProps, which remains a property of Item?

1 Answer 1

1

You can't, because there is no way in the mapping to specify that you are indexing into an array and putting that index into a specified key. I expect that this will never be supported.

Not ideal but: What you could do it to add the array property as well, with a custom setter method. When the setter is called, mutate the data by creating an instance of FrequentProps and setting the properties from the array contents.

1
  • I was afraid that this would be the case. Thanks for your suggestion, I had something like this in mind if there would be no designated use case in RestKit.
    – ff10
    Nov 26, 2013 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.