I have some troubles mapping a JSON Array with RestKit.
This is the JSON File:
"result": {"cities": [
{"city": {"id": 2, "name": "Madrid"},
"cityImages": [{"image": {"url": "",
"image": "",
"blob": "",
"title":"Madrid"}
}]
},
{"city": {"id": 11001, "name": "Seville"},
"cityImages": [{"image": {"url": "",
"image": "",
"blob": "",
"title": "Seville Foto"}},
{"image": {"url": "",
"image": "",
"blob": "",
"title": "Otra"}
}]
}]
}
Te problem is in the CityImage Array.
This is my class CityImage
@interface CityImage : NSObject
@property (nonatomic, strong) NSString *imageURL;
@property (nonatomic, strong) NSString *image;
@property (nonatomic, strong) NSString *imageBase64;
@property (nonatomic, strong) NSString *title;
@end
And this is the class City (that contains an Array of CityImages)
@interface City : NSObject
@property (nonatomic, strong) NSString *cityid;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *cityImages;
And now, this is my mapping for cityImages:
RKObjectMapping* cityImageMapping = [RKObjectMapping mappingForClass:[CityImage class] ];
[cityImageMapping mapKeyPath:@"image.url" toAttribute:@"imageURL"];
[cityImageMapping mapKeyPath:@"image.image" toAttribute:@"image"];
[cityImageMapping mapKeyPath:@"image.blob" toAttribute:@"imageBase64"];
[objectManager.mappingProvider setMapping:cityImageMapping forKeyPath:@"result.cities.cityImages"];
And for cities:
RKObjectMapping *cityMapping = [RKObjectMapping mappingForClass:[City class]];
[cityMapping mapKeyPathsToAttributes:@"city.id", @"cityid", @"city.name", @"name", nil];
[cityMapping mapKeyPath:@"cityImages.image" toAttribute:@"cityImages"];
[objectManager.mappingProvider setMapping:cityMapping forKeyPath:@"result.cities"];
When the JSON is parsed it gives me this error:
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.url'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.blob'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.url'. No strategy for transforming from '__NSArrayI' to 'NSString'
W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'image.blob'. No strategy for transforming from '__NSArrayI' to 'NSString'
I think I’ve forgot something, but although I have try almost everything I’ve found, the problem persist.
Any idea?.
Thank you for your help!!
Try setting this for the cityImageMapping:
cityImageMapping.rootKeyPath = @"image"and map your attributes asurlinstead ofimage.urlAlso, I believe you want the whole array of images instead of one image so map the cityImages array as follows:
[cityMapping mapKeyPath:@"cityImages" toAttribute:@"cityImages"];