0

我正在使用 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 可能更改的类型的组合(不是我的“原始”想法,但我有消费它)。

我已经阅读了 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;
}]; 
4

1 回答 1

1

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

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]];
于 2013-05-31T11:19:41.850 回答