RestKit 独立对象上的相同一对一映射除外

RestKit Identical To-One Mappings on Separate Objects Excepts

我知道我在做的事情很简单,但我对我在这里做错了什么感到有点困惑。

我试图让 JSON 对象上的给定键始终映射到相同的 Objective-C class。该密钥在所有具有它的 JSON 对象上具有相同的名称 (device)。

我有两个 class,FooBar,每个都有一个 device 属性:

@interface Foo
@property (strong, nonatomic) Device * device;
@end

@interface Bar
@property (strong, nonatomic) Device * device;
@end

@interface Device
@property (strong, nonatomic) NSString * deviceId;
@property (strong, nonatomic) NSString * deviceName;
@end

对于我的服务器为 FooBar 发送的 JSON,设备表示如下:

{
    "device": {
        "deviceId": "someId",
        "deviceName": "someName"
    }
}

在我的应用程序的 RestKit 配置函数中,我有以下内容:

RKObjectMapping * responseMapping = [RKObjectMapping mappingForClass: model[@"class"]];

if ( model[@"class"] == [Foo class] ||
     model[@"class"] == [Bar class] ){

    NSArray * defaultRequestKeys = @[@"id", @"createdAt", @"updatedAt"];
    NSArray * defaultModelKeys = @[@"objectId", @"createdAt", @"updatedAt"];
    NSArray * deviceRequestKeys = [@[@"deviceName", @"deviceId"] arrayByAddingObjectsFromArray: defaultRequestKeys];
    NSArray * deviceModelKeys = [@[@"deviceName", @"deviceId"] arrayByAddingObjectsFromArray: defaultModelKeys];

    RKObjectMapping * deviceResponseMapping;

    deviceResponseMapping = [RKObjectMapping mappingForClass: [Device class]];
    [deviceResponseMapping addAttributeMappingsFromDictionary: [NSDictionary dictionaryWithObjects: deviceModelKeys
                                                                                           forKeys: deviceRequestKeys]];

    [responseMapping addRelationshipMappingWithSourceKeyPath:@"device" mapping: deviceResponseMapping];

}

但是,RestKit 除外 failed: caught "NSInternalInconsistencyException", "Unable to add mapping for keyPath device, one already exists..."

这意味着我做错了什么。

我已经尝试查看所有文档,但我很困惑我如何在两个不同的对象上使用相同的键使用相同的对象映射。这是针对 FooBar 上的 Device 一对一关系。

在我的例子中,我已经在调用

之外声明了这个密钥
[responseMapping addRelationshipMappingWithSourceKeyPath:@"device" mapping: deviceResponseMapping];

在代码的另一部分。删除此密钥可解决问题。我添加它:

[responseMapping addAttributeMappingsFromDictionary: responseAttributeMappings];