【问题标题】:Restkit object mapping with multiple root objects具有多个根对象的 Restkit 对象映射
【发布时间】:2012-08-14 07:03:00
【问题描述】:

我有一个 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;
}];

【问题讨论】:

    标签: iphone objective-c cocoa-touch restkit


    【解决方案1】:

    删除线

    [responseObjectMapping setRootKeyPath:@"client"];
    

    [responseObjectMapping setRootKeyPath:@"error"];
    

    【讨论】:

    • 给我同样的错误:Request didFailLoadWithError: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Could not find an object mapping for keyPath: ''" UserInfo=0x5a56e0 {=RKObjectMapperKeyPath, NSLocalizedDescription=Could not find an object mapping for keyPath: ''}
    • 修复了它,您的评论引导我走上正确的道路,我将我的映射从 init 方法移动到为加载程序显式设置它们。编辑我的问题以显示这一点。谢谢。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    相关资源
    最近更新 更多