简体   繁体   中英

RestKit - Could not find an object mapping for keyPath: ''

The following code produce this error.

- (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];
}

I don't know how to match the ressource path with the mapping. Any idea ?

The logs are :

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]

And the json response is the following (it come from a 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"}
]

KeyPath is not the name of your json file, but the starting point for RestKit:

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

In this case, "contact" would be your keyPath: [manager.mappingProvider setMapping:contactMapping forKeyPath:@"contact"];

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