I have an object that consists of some fields such as:
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* body;
@property (nonatomic, copy) NSArray* imageUrls;
@property (nonatomic, copy) NSString* postId;
@property (nonatomic) CLLocationCoordinate2D location;
@property (nonatomic) LTUser *user;
@property (nonatomic) LTPlace *place;
@property (nonatomic, copy) NSArray* comments;
All NSString and custom objects (such as LTUser/LTPlace) and it is mapping well.
But, how can I map to the NSArray of (imageUrls - which is an array of NSString / comments - which is an array of custom objects (LTComment))?
"images": [
"http://****.com/images/1385929903887.jpg",
"http://****.com/images/131315313131.jpg",
"http://****.com/images/1351351351.jpg"
]
Mapping for main object:
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[LTUser class]];
[userMapping addAttributeMappingsFromDictionary:@{
@"_id":@"userId",
@"username":@"userName",
@"name":@"name"
}];
RKObjectMapping *placeMapping = [RKObjectMapping mappingForClass:[LTPlace class]];
[placeMapping addAttributeMappingsFromDictionary:@{
@"_id":@"placeId",
@"image":@"name",
@"name":@"image"
}];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LTPost class]];
[mapping addAttributeMappingsFromDictionary:@{
@"_id" : @"postId",
@"createdAt" : @"createdAt",
@"body" : @"body",
@"title" : @"title"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"place" toKeyPath:@"place" withMapping:placeMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodGET
pathPattern:kLTAPIGetPostsRequest
keyPath:@"posts"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
LTComment mapping
RKObjectMapping* commentMapping = [RKObjectMapping mappingForClass:[LTComment class]];
[commentMapping addAttributeMappingsFromDictionary:@{
@"user_name":@"userName",
@"text":@"text"
}];
The mapping for
commentsshould be just like you mappings foruserandplace(just with a different mapping obviously,commentMapping). RestKit will determine that the destination is a collection and that the source is a collection and do the right thing.For
imageUrls, the JSON is already an array of strings so RestKit can basically copy it. All you need to do is add to the 'container' mapping (whichever that one is):