我有一个 Web 服务,它返回以下格式的 JSON:
{
"client": {
"firstName": "Aaron"
},
"error": {
"code":""
}
}
我正在restkit中设置映射。我不断收到以下错误:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Could not find an object mapping for keyPath: ''" UserInfo=0x5961f0 {=RKObjectMapperKeyPath, NSLocalizedDescription=Could not find an object mapping for keyPath: ''}
这是我设置映射的方式:
[[RKObjectManager sharedManager].mappingProvider setMapping:[Client objectMapping] forKeyPath:@"client"];
[[RKObjectManager sharedManager].mappingProvider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];
客户端映射:
+(RKObjectMapping*) objectMapping {
RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[Client class]];
[responseObjectMapping setRootKeyPath:@"client"];
[responseObjectMapping mapKeyPathsToAttributes:
@"firstName", @"firstName",
nil];
return responseObjectMapping;
}
错误映射:
+(RKObjectMapping*) objectMapping {
RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[ErrorObject class]];
[responseObjectMapping setRootKeyPath:@"error"];
[responseObjectMapping mapKeyPathsToAttributes:
@"code", @"code",
nil];
return responseObjectMapping;
}
并像这样调用服务:
//Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
loader.userData = INITIAL_LOAD_REQUEST;
}];
编辑:
我通过以下建议解决了我的问题,并将我的映射从 init 方法移动到在对象加载器块中设置它,如下所示:
//Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
//set mapping explicitly
RKObjectMappingProvider *provider = [[RKObjectMappingProvider alloc] init];
[provider setMapping:[Clien objectMapping] forKeyPath:@"client"];
[provider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];
[loader setMappingProvider:provider];
loader.userData = INITIAL_LOAD_REQUEST;
}];