【发布时间】:2016-06-20 18:49:16
【问题描述】:
我的几个对象都有字段“id”,RestKit 不允许我将它们都映射到相同的名称。我怎样才能解决这个问题?对于这两个对象,我有一个相应的服务器端对象,其中包含需要填写的 MyId 属性。我不想将属性重命名为解决方案。
这是我可能使用的两个类的示例:
@interface MyObject : NSObject
@property NSNumber *id;
@end
@interface AnotherObject : NSObject
@property NSNumber *id;
@end
这是我将在这两个对象上运行的代码示例:
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];
这是我会得到的错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法为 keyPath MyId 添加映射,一个已经存在...”
【问题讨论】:
-
我认为您对该问题的评估不准确 - 显示
MyObject的定义并准确解释您在遇到异常时正在做什么(显示堆栈跟踪) -
更新了帖子以更具体。
-
感谢您的帮助!另外,您是否知道任何网站可以提供良好的 RestKit 教程?对它比较新。已经了解 RestKit github 教程,但想知道是否还有更多。
标签: ios objective-c restkit