我刚开始学习RestKit。我正在尝试使用它来使用Foursquare apis来附近的场地。但每次我尝试“objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)对象”我收到此警告“警告:映射嵌套对象失败:(null)”。
我真的很困惑。以下是我在init方法中的映射代码:
_objectManager = [RKObjectManager objectManagerWithBaseURL:@"https://api.foursquare.com"];
RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[Location class]];
[locationMapping mapKeyPath:@"address" toAttribute:@"address"];
[locationMapping mapKeyPath:@"city" toAttribute:@"city"];
[locationMapping mapKeyPath:@"state" toAttribute:@"state"];
[_objectManager.mappingProvider setMapping:locationMapping forKeyPath:@"location"];
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Groups class]];
[venueMapping mapRelationship:@"location" withMapping:locationMapping];
[_objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"groups"];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
[responseMapping mapRelationship:@"groups" withMapping:venueMapping];
[_objectManager.mappingProvider setMapping:responseMapping forKeyPath:@"response"];
return self;
以下是“locationManager :( CLLocationManager *)管理器中的代码didUpdateToLocation :( CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation”
NSString *resourceURLString = [NSString stringWithFormat:@"/v2/venues/search?ll=%f,%f&limit=10&client_id=LEA1MYN1Z1QWSBFIOA1T4FREVVNF0R1ADQKMA0AMGL3CN5N4&client_secret=M2W204MKEAO4KV5L4ZXUYWK2GC32PO0KT5PWYJQBP4FKBTMO",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourceURLString delegate:self];
[_locationManager stopUpdatingLocation];
最后这段代码在“objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects”
_objects = [objects retain];
NSLog(@"object that i receive is %@",objects);
以下是我在控制台中得到的回复:
2012-02-26 13:15:39.872 restKitPractice [8609:690b] W restkit.object_mapping:RKObjectMappingOperation.m:372警告:映射嵌套对象失败:(null) 2012-02-26 13:15:39.873 restKitPractice [8609:207]我收到的对象是( “回复:0x6b5cb20” )
您可以使用以下链接查看Foursquare的Venues json:
任何帮助或建议都会受到赞赏,因为我真的在努力解决这个问题。
由于 维克
答案 0 :(得分:0)
您应该查看object mapping docs。
您的映射定义与您尝试解析的json不匹配。
答案 1 :(得分:0)
我意识到这是一个老问题,但我已经看到了同样的警告。原因是您希望映射到对象的JSON类似于此{"response":{}}
。由于对象中没有键,因此它实际上映射到nil
。所以,警告是非常无害的。您将不会获得Response
对象,因为服务器实际上返回了RestKit无法映射的内容,因为JSON中没有与您的任何属性映射匹配的键。 RestKit会通过警告让您知道并回退到不创建对象。