RestKit - 找不到 keyPath 的对象映射:''

Posted

技术标签:

【中文标题】RestKit - 找不到 keyPath 的对象映射:\'\'【英文标题】:RestKit - Could not find an object mapping for keyPath: ''RestKit - 找不到 keyPath 的对象映射:'' 【发布时间】:2012-09-18 10:59:18 【问题描述】:

以下代码产生此错误。

- (void)viewDidLoad

  RKObjectManager * manager = [RKObjectManager objectManagerWithBaseURLString:@"http://localhost:3000"];
  RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[Contact class]];
  [contactMapping mapKeyPathsToAttributes:
      @"nom", @"nom",
      @"prenom", @"prenom",
      @"telephone", @"telephone",
      @"commentaires", @"description",
      nil];
  [manager.mappingProvider setMapping:contactMapping forKeyPath:@"/contacts.json"];
  [manager loadObjectsAtResourcePath:@"/contacts.json" delegate:self];

我不知道如何将资源路径与映射匹配。有什么想法吗?

日志是:

2012-09-18 11:07:30.811 MyApp[28507:fb03] I restkit:RKLog.m:33 RestKit initialized...
2012-09-18 11:07:30.816 MyApp[28507:fb03] mapping : <RKObjectMappingProvider: 0x6c1c860>
2012-09-18 11:07:30.837 MyApp[28507:fb03] I restkit.network.reachability:RKReachabilityObserver.m:391 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c51e60 host=localhost isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-18 11:07:30.886 MyApp[28507:fb03] I restkit.network:RKRequest.m:680 Updating cache date for request <RKObjectLoader: 0x6c54ec0> to 2012-09-18 11:07:30 +0000
2012-09-18 11:07:30.892 MyApp[28507:11503] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: ''
2012-09-18 11:07:30.893 MyApp[28507:11503] E restkit.network:RKObjectLoader.m:231 Encountered errors during mapping: Could not find an object mapping for keyPath: ''
2012-09-18 11:07:30.914 MyApp[28507:fb03] Error : [didFailWithError]

json 响应如下(它来自 Ruby on Rails Rest API):

[
  "commentaires":"bla bla",
   "created_at":"2012-09-17T21:15:55Z","id":1,
   "nom":"name 1",
   "prenom":"surname A",
   "telephone":"+33687330913",
   "updated_at":"2012-09-17T21:22:34Z",

  "commentaires":"bla bla",
   "created_at":"2012-09-17T21:22:20Z","id":2,
   "nom":"name 2",
   "prenom":"surname 2",
   "telephone":"",
   "updated_at":"2012-09-17T21:22:20Z"
]

【问题讨论】:

【参考方案1】:

KeyPath 不是你的 json 文件名,而是 RestKit 的起点:


    "contact": 
        "nom": "whatever nom is",
        "prenom": "same for prenom",
        // ...
    
    // ...

在这种情况下,“联系人”将是您的 keyPath: [manager.mappingProvider setMapping:contactMapping forKeyPath:@"contact"];

【讨论】:

我在我的问题中添加了 json 响应。我现在明白为什么他说密钥通行证是''。如果我了解所有内容,问题就来自我的回复...... 非常感谢。我已将我的 Ruby on Rails API 从 render :json =&gt; @contacts 更新为 render :json =&gt; :contacts =&gt; @contacts 然后我现在有一个以 contacts: 开头的 json 响应。

以上是关于RestKit - 找不到 keyPath 的对象映射:''的主要内容,如果未能解决你的问题,请参考以下文章

Restkit 无法为 keyPath 添加映射

在类型的对象上找不到 Restkit 2.0 'managedObjectContext'

RESTKit:在搜索的关键路径中找不到可映射的对象表示

关系映射中的Restkit映射keyPath

RestKit 0.2 'NSInternalInconsistencyException',原因:'无法为 keyPath scheduleEntries 添加映射,一个已经存在...'

Restkit 0.20.x cocoapods 安装问题 - 编译但找不到 RestKit 的导入