简体   繁体   中英

Restkit Unable to add mapping for keyPath

Several of my objects have the field "id" and RestKit won't let me map both of them to the same name. How can I fix this? For these two objects I have a corresponding server side object with the MyId property that needs to get filled in. I don't want to have to rename the properties as a solution.

Here is an example of two classes I may use:

@interface MyObject : NSObject
@property NSNumber *id;
@end

@interface AnotherObject : NSObject
@property NSNumber *id;
@end

Here is an example of the code that I would run on both of these objects:

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping]; 
NSDictionary *objMapping = @{@"id": @"MyId"};

[userMapping addAttributeMappingsFromDictionary:objMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[MyObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];

objMapping = @{@"id": @"MyId"};

[userMapping addAttributeMappingsFromDictionary:objMapping];
requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[AnotherObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];

Here is the error that I would get:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath MyId, one already exists...'

The problem is simply that you're calling:

[userMapping addAttributeMappingsFromDictionary:objMapping];

Multiple times with the same parameters and that isn't permitted.

In the specific case in your question you don't need that second line. In the general case you'd have some other differences in the mapping so you'd create a new mapping (and then add the dictionary of mappings to it):

[RKObjectMapping requestMapping] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM