关于从服务器获取的JSON数据为OC关键字的解决方法(id, description)

来源:互联网 发布:家用配电箱品牌知乎 编辑:程序博客网 时间:2024/06/01 09:29

在实际开发中,我们经常会从服务器提供的 JSON 数据中得到 id,description 等 OC 常见的关键字。

@property (nonatomic, strong) NSString *id;@property (nonatomic, strong) NSString *description;

此时如果我们进行字典转模型的操作,肯定会出现问题。
而我们知道可以通过重写系统提供的方法来解决此类问题

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

但如果使用的是第三方框架如:YYModel,MJExtension 我们该怎么办呢?
其实这些第三方框架也提供了一些方法用于解决这些此类问题。

在模型的.m文件中引入第三方框架提供的类方法

// 如果是YYModel+ (NSDictionary *)modelCustomPropertyMapper {    return @{@"xxxxx" :@"id",             @"xxxxx" : @"description"};}// 如果是MJExtension+ (NSDictionary *)replacedKeyFromPropertyName{    return @{@"xxxxx" :@"id",             @"xxxxx" : @"description"};}

并将声明中的 OC 关键字进行替换

@property (nonatomic,strong) NSString *xxxxx;
0 0