RestKit mapKeyPath 到数组索引

发布于 2024-11-24 09:47:09 字数 856 浏览 1 评论 0原文

我想使用 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 中的位置数组中的值映射到 Objective-C 类的属性中。到目前为止,映射看起来像这样:

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

现在如何添加纬度/经度的映射?我尝试了各种方法,但不起作用。例如:

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

有没有办法将 JSON 中的 position[0] 映射到我的对象中的 latitude

I would like to map a given array index into a property with RestKit (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 can't figure out how to map values out of the position array in my JSON into properties of my objective-c class. The mapping looks like this so far:

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

Now how can I add a mapping for the latitude/longitude? I tried various things, that don't work. e.g.:

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

Is there a way to map position[0] out of the JSON into latitude in my object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-12-01 09:47:11

简短的答案是否定的 - 键值编码不允许这样做。对于集合,仅支持 max、min、avg、sum 等聚合操作。

您最好的选择可能是将 NSArray 属性添加到 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

并像这样定义映射:

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

之后,您可以从坐标手动分配纬度和经度。

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

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

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

The short answer is no - key-value coding doesn't allow for that. For collection, only aggregate operations such as max, min, avg, sum are supported.

Your best bet is probably to add an 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 mapping like this:

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

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

EDIT: A good place to do latitude/longitude assignment is probably in object loader delegates

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

and

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文