【问题标题】:Restkit ObjectMappingRestkit 对象映射
【发布时间】:2015-04-09 13:23:13
【问题描述】:

我有一个如下的json

    {  
   "predictions":[  
      {  
         "description":"User1",
         "id":"75b8c57b4443f4b881f0efc94afd52437232aee9"
      },
      {  
         "description":"User2",
         "id":"82aa3303704041cda0e4fc34024d383dbf923604"
      },
      {  
         "description":"User3",
         "id":"f88f669a30934ef599bccf86c0236426cf2d313e"
      },
      {  
         "description":"User4",
         "id":"fa9ae7b65fa52bffdabf00d69e7db5cd37539781"
      },
      {  
         "description":"User5",
         "id":“66df3fd7e400eb31efd3ac935036aab2c02b03f0"
      }
   ],
   "status":"OK"
}

我正在使用 RestKit 来处理它。下面是我如何进行对象映射

第 1 类 - 描述.h

@interface Description : NSObject

@property(nonatomic,strong) NSString* place;

@end

第 2 类 - Predictions.h

@interface Predictions : NSObject

@property(nonatomic,strong) Description* placeDescription;

@end

在我的网络界面中,我这样做了

-(RKResponseDescriptor*)rkObjectMappingforAutoSuggest:(NSString**)objectPath{

    RKObjectMapping* descriptionMapping = [RKObjectMapping mappingForClass:[Predictions class]];
    [descriptionMapping addAttributeMappingsFromDictionary:@{
                                                          @"description":@"description"}];

    RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[Description class]];
    [responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"predictions"
                                                                                    toKeyPath:@"predictions"
                                                                                  withMapping:descriptionMapping]];

    RKResponseDescriptor *responseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                 method:RKRequestMethodGET
                                            pathPattern: objectPath
                                                keyPath:nil
                                            statusCodes:[NSIndexSet indexSetWithIndex:200]];

    return responseDescriptor;
}

我只需要回复中的描述。

我想我在数据映射中犯了一些错误。应用程序因以下消息而崩溃

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Predictions 0x15ef98d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description.'
***

无法找出问题所在。

【问题讨论】:

    标签: ios objective-c json restkit


    【解决方案1】:

    您将 JSON 中的 description 键映射到不存在的 Predictions 对象上的 description 键。

    Predictions 类的属性称为placeDescription,而不是description

    将传递给addAttributeMappingsFromDictionary: 的字典替换为:

    @{
        @"description":@"placeDescription"
    }
    

    【讨论】:

    • 我在某处错了。我没有收到回复
    • 我没想到如果 RestKit 没有找到对 map 的响应会抛出异常。这个答案是否会阻止抛出异常?如果您没有收到来自服务器的响应,那就是您的原始问题提出的映射问题。
    • 我的映射不正确。我未映射“状态”。这就是问题所在。
    • 好的。但是您的实际问题是关于抛出异常的问题,因为您试图为键 description 设置一个值,这个答案应该已经解决了。
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2017-12-15
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多