RestKit 0.22 : how to map only firstObject of an array?

假如想象 提交于 2019-12-11 04:47:08

问题


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"] }

With the following model:

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *firstTag;

To map name, I have this code:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"MyObjectModel" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:@{@"name": @"name"}];

To map firstObject of tags to firstTag, I do not know how to proceed. Note: MyObjectModel is an NSManagedObject, so firstTag is @dynamic.


回答1:


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.



来源:https://stackoverflow.com/questions/22145704/restkit-0-22-how-to-map-only-firstobject-of-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!