【问题标题】:Restkit Unable to add mapping for keyPathRestkit 无法为 keyPath 添加映射
【发布时间】: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


【解决方案1】:

问题只是你在打电话:

[userMapping addAttributeMappingsFromDictionary:objMapping];

多次使用相同的参数,这是不允许的。

在您问题的特定情况下,您不需要第二行。在一般情况下,您的映射会有一些其他差异,因此您需要创建一个新映射(然后将映射字典添加到其中):

[RKObjectMapping requestMapping] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多