gpt4 book ai didi

iphone - 具有多个根对象的 Restkit 对象映射

转载 作者:行者123 更新时间:2023-12-03 21:03:17 25 4
gpt4 key购买 nike

我有一个 Web 服务,它返回以下格式的 JSON:

{
"client": {
"firstName": "Aaron"
},
"error": {
"code":""
}
}

我正在restkit 中设置映射。我不断收到以下错误:

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001“无法找到 keyPath 的对象映射:''” UserInfo=0x5961f0 {=RKObjectMapperKeyPath,NSLocalizedDescription=无法找到 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 方法移动到对象加载器 block 中设置它,如下所示:

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

最佳答案

删除线条

[responseObjectMapping setRootKeyPath:@"client"];

[responseObjectMapping setRootKeyPath:@"error"];

关于iphone - 具有多个根对象的 Restkit 对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11947306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com