RestKit mapKeyPath to Array index

I would like to map this array index to the RestKit property (OM2). I have this JSON:

{ "id": "foo", "position": [52.63, 11.37] } 

which I would like to map to this object:

 @interface NOSearchResult : NSObject @property(retain) NSString* place_id; @property(retain) NSNumber* latitude; @property(retain) NSNumber* longitude; @end 

I cannot figure out how to match the values โ€‹โ€‹from the position array in my JSON in the properties of my objective-c class. The display looks like this:

 RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]]; [resultMapping mapKeyPath:@"id" toAttribute:@"place_id"]; 

Now, how can I add a mapping for latitude / longitude? I have tried different things that do not work. eg:.

 [resultMapping mapKeyPath:@"position[0]" toAttribute:@"latitude"]; [resultMapping mapKeyPath:@"position.1" toAttribute:@"longitude"]; 

Is there a way to display position[0] from JSON to latitude in my object?

+7
source share
1 answer

The short answer is no - key-value encoding does not allow this. Only aggregated operations, such as max, min, avg, sum, are supported for collection.

It is best to add the NSArray property to NOSearchResult:

 // NOSearchResult definition @interface NOSearchResult : NSObject @property(retain) NSString* place_id; @property(retain) NSString* latitude; @property(retain) NSNumber* longitude; @property(retain) NSArray* coordinates; @end @implementation NOSearchResult @synthesize place_id, latitude, longitude, coordinates; @end 

and define the mapping as follows:

 RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]]; [resultMapping mapKeyPath:@"id" toAttribute:@"place_id"]; [resultMapping mapKeyPath:@"position" toAttribute:@"coordinates"]; 

After that, you can manually assign latitude and longitude from the coordinates.

EDIT: A good place to assign latitude / longitude, probably in object loader delegates

 - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object; 

and

 - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects; 
+3
source

All Articles