DCKeyValueObjectMapping JSON到Object映射错误

时间:2016-05-18 11:37:13

标签: ios objective-c objectmapper

在Objective C for Object mapping中我正在使用库DCKeyValueObjectMapping

这是我的班级

#import <Foundation/Foundation.h>

@interface City : NSObject
@property(nonatomic, strong) NSString *id;
@property(nonatomic, strong) NSString *title;
@property(nonatomic, strong) NSString *slug;
@property(nonatomic) int country;
@property(nonatomic) Boolean isMain;
@property(nonatomic, strong) NSString *description;
@property(nonatomic, strong) NSString *position;
@property(nonatomic) Boolean isActive;
@property(nonatomic) Boolean isVisible;
@end

以下是我解析回复的方法

- (void)getCities {
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@city/", APIURL]];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:URL.absoluteString
      parameters:nil
        progress:nil
         success:^(NSURLSessionTask *task, id responseObject) {
             DCParserConfiguration *config = [DCParserConfiguration configuration];
             DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [City class] andConfiguration: config];
             NSArray *cities = [parser parseArray: responseObject];
    }failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error.localizedDescription);
    }];
}

以下是来自服务器的回复:

(
        {
        country = 1;
        description = 321;
        id = 1;
        "is_active" = 1;
        "is_main" = 0;
        "is_visible" = 1;
        position = "POINT (42.8760663999999991 74.5912887000000069)";
        slug = blabla;
        title = "\U0411\U0438\U0448\U043a\U0435\U043a";
    },
        {
        country = 1;
        description = 123;
        id = 2;
        "is_active" = 1;
        "is_main" = 0;
        "is_visible" = 1;
        position = "POINT (40.5266999999999982 72.8031000000000006)";
        slug = bloblo;
        title = "\U041e\U0448";
    }
)

当我调用此方法时,每次对象映射都会崩溃。

以下是调试消息:

***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不符合密钥描述的密钥值编码。'

将属性描述从JSON转换为对象时崩溃。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

最后我发现了问题

问题是,我在我的类中声明了属性“description”,它与父类的属性冲突。 在这种情况下,您声明属性描述Xcode将显示警告消息。

要解决此问题,您需要添加到您的实现文件.m一行代码:

@synthesize description;