Iphone RestKit mapKeyPath到数组索引

Iphone RestKit mapKeyPath到数组索引,iphone,objective-c,json,restkit,Iphone,Objective C,Json,Restkit,我想用RestKit(OM2)将给定的数组索引映射到属性中。我有一个JSON: { "id": "foo", "position": [52.63, 11.37] } 我想将其映射到此对象: @interface NOSearchResult : NSObject @property(retain) NSString* place_id; @property(retain) NSNumber* latitude; @property(retain) NSNumber* longitud

我想用RestKit(OM2)将给定的数组索引映射到属性中。我有一个JSON:

{
  "id": "foo",
  "position": [52.63, 11.37]
}
我想将其映射到此对象:

@interface NOSearchResult : NSObject
@property(retain) NSString* place_id;
@property(retain) NSNumber* latitude;
@property(retain) NSNumber* longitude;
@end
我不知道如何将JSON中的position数组中的值映射到objective-c类的属性中。到目前为止,映射如下所示:

RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]];
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"];
现在如何添加纬度/经度的映射?我试过各种各样的方法,但都不管用。e、 g:

[resultMapping mapKeyPath:@"position[0]" toAttribute:@"latitude"];
[resultMapping mapKeyPath:@"position.1" toAttribute:@"longitude"];
有没有办法将JSON中的位置[0]映射到我的对象中的纬度

简单的答案是否定的——这是不允许的。对于集合,仅支持聚合操作,如max、min、avg、sum

您最好的选择可能是向NOSearchResult添加NSArray属性:

// 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
并定义如下所示的映射:

RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]];
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"];
[resultMapping mapKeyPath:@"position" toAttribute:@"coordinates"];
之后,您可以根据坐标手动指定纬度和经度

编辑:进行纬度/经度分配的一个好地方可能是在对象加载器委托中

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


谢谢-我已经担心它不起作用了。“didLoadObject”提示非常有用!更好的地方是用于lat和lon的自定义getter和setter,它们操作底层数组数据结构。
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;