Restkit将对象映射到NSArray

时间:2013-12-20 15:38:33

标签: ios nsarray restkit rkobjectmapping

我有一个包含某些字段的对象,例如:

@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)并且映射效果良好。

但是,我如何映射到NSArrayimageUrls - 这是一个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];

1 个答案:

答案 0 :(得分:2)

comments的映射应该与userplace的映射一样(显然只是使用不同的映射,commentMapping)。 RestKit将确定目标是一个集合,并且源是一个集合并做正确的事。

对于imageUrls,JSON已经是一个字符串数组,因此RestKit基本上可以复制它。您需要做的就是添加到'容器'映射(无论哪个):

@"images" : @"imageUrls"