1
votes

My JSON looks like this:

[ [value1, value2, value3], [value1, value2, value3]]

I want to iterate over the external array, and map each internal array to an object such as:

@interface MyObject : NSObject
    @property (nonatomic, copy) NSString* key1;
    @property (nonatomic, copy) NSString* key2;
    @property (nonatomic, copy) NSString* key3;
@end

For the sample JSON, I should get two mapped objects, e.g. MyObject1, MyObject2, where MyObject1 is mapped to the first internal array, and MyObject2 is mapped to the second internal array. Each having their properties mapped to corresponding values in the array i.e. key1 == value1, key2==value2 and key3==value3.

Any ideas hot to do such a mapping?

1
I'm not sure what you're asking. You want to store these arrays of values into arrays? So you'd have Array1, Array2, etc. containing 3 strings every time? - Gil Sand
could you give a sample of your json ? - Smiless
Modified my question. Please let me know if it still isn't clear. - Shuaib

1 Answers

0
votes

The way I ended up handling it was to add an array property to my object, and map the whole array to that property (a transformable property incase of a NSManagedObject subclass)

RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"MyObject" inManagedObjectStore:managedObjectStore];

[responseMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"values"]];

After mapping is complete, I assign rest of the values in the completion block:

^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        [mappingResult.array enumerateObjectsUsingBlock:^(MyObject *obj, NSUInteger idx, BOOL *stop) {
            obj.key1 = obj.values[0];
            obj.key2 = obj.values[1];
            obj.key3 = obj.values[2];
        }];