2014-03-03 181 views
1

json api有一個帖子列表,每個帖子都有一個像這個帖子一樣的用戶列表。列表中的RestKit對象映射

後,用戶每個核心數據都有一個實體。

RestKit如何做對象映射。

{ 
    posts:[ 
     { 
      "postid": 1, 
      "posttext": "post1", 
      "likeusers":[ 
       { 
        "uid": 1, 
        "username": "user1" 
       }, 
       { 
        "uid": 2, 
        "username": "user2" 
       } 
      ] 
     }, 
     { 
      "postid": 2, 
      "posttext": "post2", 
      "likeusers":[ 
       { 
        "uid": 1, 
        "username": "user1" 
       }, 
       { 
        "uid": 2, 
        "username": "user2" 
       } 
      ] 
     } 
    ] 
} 
+0

我讀了一本書「RestKit for iOS」,然後閱讀了RestKit文檔,在短時間內獲得了太多信息,並且不知道這個問題。下次再提問時會做出嘗試。 – lbsweek

回答

1

這裏是如何做到的映射:

// User mapping 
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" 
                inManagedObjectStore:yourManagedObjectStore]; 
userMapping.identificationAttributes = @[@"userID"]; 
[userMapping addAttributeMappingsFromDictionary:@{ 
                @"uid": @"userID", 
                @"username": @"username", 
                }]; 

// Post mapping 
RKEntityMapping *postMapping = [RKEntityMapping mappingForEntityForName:@"Post" 
                inManagedObjectStore:yourManagedObjectStore]; 
postMapping.identificationAttributes = @[@"postID"]; 
[postMapping addAttributeMappingsFromDictionary:@{ 
                @"postid": @"postID", 
                @"posttext": @"postText", 
                }]; 

[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"likeusers" 
                      toKeyPath:@"likeUsers" withMapping:userMapping]]; 

希望它能幫助。