gpt4 book ai didi

ios - 将对象映射到键/值列表

转载 作者:行者123 更新时间:2023-11-29 02:51:25 24 4
gpt4 key购买 nike

我有一个对象列表,我想将其映射到 json,如下所示:

"tags": {
"f6a34fea-5d06-4fb2-a2c7-127e2b58165a": true,
"39413ca6-b817-4ede-abd3-35822aec91b6": true
},

我目前拥有的:

RKObjectMapping *tagRequestMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[tagRequestMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"id"];
[tagRequestMapping addAttributeMappingsFromDictionary:@{@"hasTasks" : @"(id)"}];
tagRequestMapping.forceCollectionMapping = NO;
[taskRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"tags"
toKeyPath:@"tags"
withMapping:tagRequestMapping]];

这会产生以下 json:

"tags": [
{
"f6a34fea-5d06-4fb2-a2c7-127e2b58165a": true
},
{
"39413ca6-b817-4ede-abd3-35822aec91b6": true
}
],

不幸的是,webapp 不认为这是有效的。我使用相同的规则将数据从 json 映射到我的核心数据对象,它工作得很好。但不适用于 Core Data 到 json。我将如何修改我的映射,以获取具有所有值的单个对象,就像指定的那样?

最佳答案

感谢Wain我想出了一个解决方案:

我向我的 Task 类添加了一个新属性,它是只读的并且有一个自定义的 getter。在那个 getter 中,我从 Tag 对象的 NSSet 创建了一个字典。然后对于映射部分,我只是将我的新属性添加为一个应该映射的普通属性,它工作得很好。

自定义 setter/getter :

- (NSDictionary *)getTagDictionary {
NSMutableDictionary *tagDictionary = [NSMutableDictionary dictionary];
for (Tag *tag in self.tags) {
[tagDictionary setObject:tag.hasTasks forKey:tag.id];
}
return tagDictionary;
}

映射:

[taskRequestMapping addAttributeMappingsFromDictionary:@{
@"tagDictionary":@"tags"}];

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

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