ios - Restkit对象映射

标签 ios objective-c json restkit

我有一个如下所示的 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.'
***

无法找出问题。

最佳答案

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

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

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

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

关于ios - Restkit对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539749/

相关文章:

ios - 在 SwiftUI 中构建 ListView 时出错

javascript - ReactJS - 获取函数: Why am I getting uncaught (in promise) SyntaxError: Unexpected end of JSON input?

ios - iOS 上的阴影更强

iOS 7 supportedInterfaceOrientation 没有按预期工作

ios - 切片字符串 Swift

iphone - 将参数发送到 UIWebView 中的本地文件

ios - Xcode 中的第 3 方框架编译警告

ios - Objective - C 获取 CFBundleShortVersionString 的空白值 - 应用程序版本

php - highcharts JSON 谷歌地球

javascript - Ember.js ember-data restadapter 无法加载 json