有时候后台json数据返回的字段含有“id”,也有可能是有时候为了减少代码的冗余,两页面之间只是数据模型个别属性的区别,所以这时候最好是用到模型属性的替换,用新的属性替换返回的json数据的字段。这里主要总结了两种使用方法,都是在项目已经集成别人封装好的解析json数据的库的基础上。
以常见YimaiMaterialsAddressModel模型为例,需要用addressId替换掉json数据里面的id字段
@interface YimaiMaterialsAddressModel : NSObject
@property (nonatomic, copy) NSString *addressId;
@property (nonatomic, copy) NSString *uname;
@property (nonatomic, copy) NSString *mobile;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *county;
@property (nonatomic, copy) NSString *address;
@end
1.集成了MJExtension,所以需要处理下.m文件
#import "YimaiMaterialsAddressModel.h" @implementation YimaiMaterialsAddressModel
+ (NSDictionary *)replacedKeyFromPropertyName{
return @{@"addressId" : @"id"};
}
@end
具体解析的使用方法
self.addressModel = [YimaiMaterialsAddressModel objectWithKeyValues:request.responseObject[@"data"]];
2.集成了YYKit,所以需要处理.m文件
#import "YimaiMaterialsAddressModel.h" @implementation YimaiMaterialsAddressModel
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"addressId" : @"id"};
}
@end
具体解析的使用方法
self.addressModel = [YimaiMaterialsAddressModel modelWithDictionary:request.responseObject[@"data"]];