gpt4 book ai didi

ios - 列表中列表的 RestKit 对象映射

转载 作者:行者123 更新时间:2023-11-29 12:52:15 24 4
gpt4 key购买 nike

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

post,用户每人在核心数据中都有一个实体。

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

最佳答案

下面是映射的方法:

// 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]];

希望对您有所帮助。

关于ios - 列表中列表的 RestKit 对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22155333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com