gpt4 book ai didi

ios - RestKit 0.20 : flattening the hierarchy with dynamic in JSON

转载 作者:行者123 更新时间:2023-11-29 03:53:01 25 4
gpt4 key购买 nike

我正在使用 RestKit 0.20,并且在将包含动态键的具有层次结构的 JSON 映射到一个对象时遇到问题。

JSON 如下所示:

{
"id": 42,
"name": "Name of this entity",
"specialDataMap": {
"2091:10": {
"id": 2091,
"type": "10",
"value": "1'509.49",
"name": "Name of special data type 10"
},
"2091:02" {
"id": 2091,
"type": "02",
"value": "5.5543",
"name": "Name of special data type 02"
}
}
}

并且应该使用 RestKit 映射到这样的对象:

@interface InfoPoint : NSManagedObject

@property (nonatomic, retrain) NSString* identifier;
@property (nonatomic, retrain) NSString* name;
@property (nonatomic, retrain) NSString* valueOfType10;
@property (nonatomic, retrain) NSString* valueOfType02;

@end

如您所见,我不想创建关系并将特殊数据存储到单独的对象中。这根本没有意义。我想像所有其他属性一样将嵌套属性分配到 InfoPoint 对象中。通常这适用于嵌套对象的关键路径,但该路径包含动态部分:“2091:10”是 id 和 id 可能更改的类型的组合(不是我的“原始”想法,但我有消耗它)。

我已阅读有关 Handling Dynamic Nesting Attributes 的内容在 RestKit 文档中。但我没有找到这是否以及如何与嵌套属性一起使用。

----- 添加为评论/问题的回复:----

我也尝试过 Dynamic Object Mapping .但它不起作用,因为 RestKit 似乎对 @"self"目的地有问题:

RKEntityMapping* type10Mapping = [RKEntityMapping mappingForEntityForName:@"InfoPoint" inManagedObjectStore:objectStore];
[type10Mapping addAttributeMappingsFromDictionary:@{
@"value": @"valueOfType10"}];

RKDynamicMapping* dynamicMapping = [[RKDynamicMapping alloc] init];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"specialDataMap" toKeyPath:@"self" withMapping:dynamicMapping]];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
if ([[representation valueForKeyPath:@"type"] isEqualToString:@"10"]) {
return type10Mapping;
}
return nil;
}];

最佳答案

您对动态映射的尝试不正确。您应该提供到响应描述符的动态映射。然后,动态映射应根据在响应中找到的键创建并返回适当的映射。像(徒手写的):

RKDynamicMapping* dynamicMapping = [[RKDynamicMapping alloc] init];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

RKEntityMapping* typeMapping = [RKEntityMapping mappingForEntityForName:@"InfoPoint" inManagedObjectStore:objectStore];
[typeMapping addAttributeMappingsFromDictionary:@{
@"id" : @"identifier",
@"name": @"name",}];

NSDictionary *types = [representation valueForKeyPath:@"specialDataMap"];

for (NSString *key in types) {
NSDictionary *type = [[types objectForKey:key] objectForKey:@"type"];
if ([type isEqualToString:@"10"]) {
[typeMapping addAttributeMappingsFromDictionary:@{
[NSString stringWithFormat:@"specialDataMap.%@.value", key]: @"valueOfType10"}];
} else if ...
}

return typeMapping;
}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping
pathPattern:...
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];

关于ios - RestKit 0.20 : flattening the hierarchy with dynamic in JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16851151/

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