无法映射嵌套的 RestKit 对象

Posted

技术标签:

【中文标题】无法映射嵌套的 RestKit 对象【英文标题】:Can't map nested RestKit object 【发布时间】:2014-02-03 07:45:44 【问题描述】:

我有一个问题,无法映射 json 的嵌套对象。问题出在“容量”键上。

这是错误:

restkit.object_mapping:RKMappingOperation.m:830 Did not find mappable relationship value keyPath 'capacities'

我知道问题出在映射本身,但就是想不通:

RKObjectMapping *capacityMapping = [RKObjectMapping mappingForClass:[Capacity class]];
[capacityMapping addAttributeMappingsFromDictionary:@ @"capacityText" : @"capacityText",
                                                       @"priceDescriptionText" : @"priceDescriptionText",
                                                       @"priceText" : @"priceText" ];

RKObjectMapping *colorsMapping = [RKObjectMapping mappingForClass:[Colors class]];
[colorsMapping addAttributeMappingsFromDictionary:@ @"ID" : @"idNum",
                                                     @"Name" : @"name",
                                                     @"colorHex" : @"colorHex",
                                                     @"imageUrl" : @"imageURL" ];

RKObjectMapping *deviceDataMapping = [RKObjectMapping mappingForClass:[DeviceData class]];
[deviceDataMapping addAttributeMappingsFromDictionary:@ @"device.ID" : @"idNum",
                                                         @"device.Name" : @"name",
                                                         @"device.additionalFeatures" : @"additionalFeatures",
                                                         @"device.deviceName" : @"deviceName",
                                                         @"device.mainFeatures" : @"mainFeatures",
                                                         @"device.supportPagesLinks" : @"supportPagesLinks",
                                                         @"device.whatsInTheKit" : @"whatsInTheKit" ];

[deviceDataMapping addRelationshipMappingWithSourceKeyPath:@"capacities" mapping:capacityMapping];

[capacityMapping addRelationshipMappingWithSourceKeyPath:@"colors" mapping:colorsMapping];

[deviceDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"responseError"
                                                                                  toKeyPath:@"responseError"
                                                                                withMapping:errorMapping]];

[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:deviceDataMapping
                                                                                                    method:RKRequestMethodGET
                                                                                               pathPattern:@"devices/:boneID"
                                                                                                   keyPath:nil
                                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

这是DeviceData 类:

@interface DeviceData : NSObject
@property (nonatomic) ResponseError *responseError;
@property (copy, nonatomic) NSString *idNum;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *additionalFeatures;
@property (copy, nonatomic) NSString *deviceName;
@property (copy, nonatomic) NSString *mainFeatures;
@property (copy, nonatomic) NSString *supportPagesLinks;
@property (copy, nonatomic) NSString *whatsInTheKit;
@property (nonatomic) NSArray *capacities;
@property (assign, nonatomic) NSInteger boneID;
@end

这是Capacity 类:

@interface Capacity : NSObject
@property (copy, nonatomic) NSString *capacityText;
@property (nonatomic) NSArray *colors;
@property (copy, nonatomic) NSString *priceDescriptionText;
@property (copy, nonatomic) NSString *priceText;
@end

这是Colors 类:

@interface Colors : NSObject
@property (copy, nonatomic) NSString *idNum;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *colorHex;
@property (copy, nonatomic) NSString *imageURL;
@end

这是json:


  "responseError": null,
  "device": 
    "ID": null,
    "Name": null,
    "additionalFeatures": "additional features text",
    "capacities": [
      
        "capacityText": "16GB",
        "colors": [
          
            "ID": null,
            "Name": null,
            "colorHex": "#a68f76",
            "imageUrl": "iphone_5s_black.png"
          ,
          
            "ID": null,
            "Name": null,
            "colorHex": "#a9a9a9",
            "imageUrl": "iphone_5s_black.png"
          ,
          
            "ID": null,
            "Name": null,
            "colorHex": "#616065",
            "imageUrl": "iphone_5s_black.png"
          
        ],
        "priceDescriptionText": “iPhone 5S",
        "priceText": "750$"
      ,
        "capacityText": “32GB",
        "colors": [
          
            "ID": null,
            "Name": null,
            "colorHex": "#a68f76",
            "imageUrl": "iphone_5s_black.png"
          ,
          
            "ID": null,
            "Name": null,
            "colorHex": "#a9a9a9",
            "imageUrl": “iphone_5s_black.png"
          ,
          
            "ID": null,
            "Name": null,
            "colorHex": "#616065",
            "imageUrl": “iphone_5s_black.png"
          
        ],
        "priceDescriptionText": “iPhone 5S",
        "priceText": "750$"
      
    ],
    "deviceName": "iPhone 5s",
    "mainFeatures": “some main features text",
    "supportPagesLinks": [
      
        "linkText": “restore",
        "linkUrl": “restore.pdf"
      ],
    "whatsInTheKit": "what inside the kit text"
  

【问题讨论】:

【参考方案1】:

因为您的响应描述符有一个 nil 键路径,并且您在 deviceMapping 的所有源键路径中使用 device.xxx,所以您需要有一个明确的关系映射,以便您可以指定源和目标键路径:

RKRelationshipMapping *capacitiesMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"device.capacities" toKeyPath:@"capacities" withMapping:capacityMapping];
[deviceDataMapping addPropertyMapping:capacitiesMapping];

【讨论】:

是的,我已经这样做了,但是没有成功,结果是:*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RKDeviceData 0x165647f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key device.',这是为什么呢? 是的,我猜你犯了一个错误,并且已经尝试过了,而且效果很好。无论如何感谢您的帮助,这很有帮助。 ...我已经知道该怎么做了! - 但错过了一个事实,即在 API 中,一条数据已被下移……无论哪种方式,您的回答都提醒我再次检查,所以谢谢!

以上是关于无法映射嵌套的 RestKit 对象的主要内容,如果未能解决你的问题,请参考以下文章

Restkit 0.20 嵌套对象数组映射问题

RestKit:将嵌套数组映射到对象

RestKit 0.20 嵌套对象映射(对象树的路径不同)

Restkit嵌套对象映射问题

RestKit 中的嵌套对象映射

RestKit 将未嵌套关系映射到拥有对象