0
votes

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

1 Answers

1
votes

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.