2013-12-20 322 views
1

我有一個包含一些領域如的目的:Restkit將對象映射到的NSArray

@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),並且映射良好。

但是,如何可以映射到的(imageUrls - 這是NSString/comments陣列 - 這是自定義對象(LTComment)的陣列)的NSArray

"images": [ 
     "http://****.com/images/1385929903887.jpg", 
     "http://****.com/images/131315313131.jpg", 
     "http://****.com/images/1351351351.jpg" 
] 

映射爲主要對象:

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映射

RKObjectMapping* commentMapping = [RKObjectMapping mappingForClass:[LTComment class]]; 
[commentMapping addAttributeMappingsFromDictionary:@{ 
                @"user_name":@"userName", 
                @"text":@"text" 
                }]; 
+0

顯示您的當前映射。和源JSON。您是否已經爲LTComment製作了映射?後 – Wain

+0

添加描述「映射爲主要目的:」 –

+0

添加LTComment映射。 –

回答

2

comments的映射應該像你一樣映射userplace(只是用不同的映射明顯,commentMapping)。 RestKit會確定目標是一個集合,並且源代碼是一個集合並執行正確的操作。

對於imageUrls中,JSON已經是一個字符串數組,以便RestKit可以基本上覆制它。您只需添加到'容器'映射(無論哪一個):

@"images" : @"imageUrls" 
+0

謝謝你的回答! –