RestKit列表中列表的对象映射

时间:2014-03-03 19:17:50

标签: ios restkit

json api有一个帖子列表,每个帖子都有一个喜欢这篇帖子的用户列表。

发布,每个用户都有核心数据中的实体。

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"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分: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]];

希望它有所帮助。

相关问题