我有一个包含某些字段的对象,例如:
@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;
所有NSString
和自定义对象(例如LTUser
/ LTPlace
)并且映射效果良好。
但是,我如何映射到NSArray
(imageUrls
- 这是一个NSString
/ comments
数组 - 这是一个自定义对象数组({ {1}}))?
LTComment
主要对象的映射:
"images": [
"http://****.com/images/1385929903887.jpg",
"http://****.com/images/131315313131.jpg",
"http://****.com/images/1351351351.jpg"
]
LTComment映射
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];
答案 0 :(得分:2)
comments
的映射应该与user
和place
的映射一样(显然只是使用不同的映射,commentMapping
)。 RestKit将确定目标是一个集合,并且源是一个集合并做正确的事。
对于imageUrls
,JSON已经是一个字符串数组,因此RestKit基本上可以复制它。您需要做的就是添加到'容器'映射(无论哪个):
@"images" : @"imageUrls"