RestKit 0.22:如何仅映射数组的firstObject? [英] RestKit 0.22 : how to map only firstObject of an array?
问题描述
我收到的数据超出了所需:我只想映射对象数组中的第一个对象. 使用RestKit 0.22,如何映射以下Json:
I am receiving more data than needed: I only want to map first object of an array of objects. Using RestKit 0.22, how to map the following Json:
{ "name": "foobar",
"tags": ["tag1", "tag2", "tag3"] }
使用以下模型:
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *firstTag;
要映射name
,我有以下代码:
To map name
, I have this code:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"MyObjectModel" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:@{@"name": @"name"}];
要将tags
的firstObject映射到firstTag
,我不知道如何进行.注意:MyObjectModel
是NSManagedObject
,所以firstTag
是@dynamic
.
To map firstObject of tags
to firstTag
, I do not know how to proceed. Note: MyObjectModel
is an NSManagedObject
, so firstTag
is @dynamic
.
推荐答案
久经考验的方法是在采用完整数组的托管对象上创建一个方法.该方法提取第一项并将其保存到实际的NSString
属性.然后,您的映射就成为所有数据类型都匹配的标准.
The tried and tested approach is to create a method on the managed object which takes the full array. That method extracts the first item and saves it to the actual NSString
property. Then your mapping is as standard as all of the data types match.
这篇关于RestKit 0.22:如何仅映射数组的firstObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!