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

标签 iphone objective-c cocoa-touch restkit

我有一个 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/

相关文章:

iphone - 如何制作自定义通知,以便每当事件发生时它都会在 iOS 中处理

iphone - 如何计算 Interface Builder Color 的 html 颜色

ios - 检查 ios 是否有可用的互联网

ios - 仅将触摸事件转发给被触摸的 View

iphone - 使用核心数据按工作日分组

iphone - Interface Builder 中的 UITabBarControllers 遇到问题

iphone - 这是正确的代码重新存储管理-对于基本的自定义类

ios - 为什么用 NSString *str1 = [[NSString alloc]init]; 分配两个字符串有相同的地址吗?

objective-c - 在 dismissModalViewController 中自定义 UIView 动画

iphone - super View 如何在其任何 subview 之前拦截触摸序列?