0

在用于对象映射的 Objective C 中,我使用库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 转换为对象时崩溃。我在这里做错了什么?

4

1 回答 1

0

最后我发现了问题

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

要解决这个问题,你需要在你的实现文件 .m 中添加一行代码:

@synthesize description;
于 2016-05-24T06:04:42.207 回答