DCKeyValueObjectMapping JSON 到对象映射错误
DCKeyValueObjectMapping JSON to Object mapping error
在 Objective C 中,对于对象映射,我正在使用库 DCKeyValueObjectMapping
这是我的 class
#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:]:此 class 不符合密钥描述的键值编码。 '
将 属性 描述从 JSON 转换为对象时崩溃。我在这里做错了什么?
终于找到问题了
问题是,我在我的 class 中声明了 属性 "description",这与父 class 的 属性 冲突。
在这种情况下,您声明 属性 的描述 Xcode 将显示一条警告消息。
要解决这个问题,需要在你的实现文件.m中添加一行代码:
@synthesize description;
在 Objective C 中,对于对象映射,我正在使用库 DCKeyValueObjectMapping
这是我的 class
#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:]:此 class 不符合密钥描述的键值编码。 '
将 属性 描述从 JSON 转换为对象时崩溃。我在这里做错了什么?
终于找到问题了
问题是,我在我的 class 中声明了 属性 "description",这与父 class 的 属性 冲突。 在这种情况下,您声明 属性 的描述 Xcode 将显示一条警告消息。
要解决这个问题,需要在你的实现文件.m中添加一行代码:
@synthesize description;