一,库名称:MJExtension
二,pod引入:
pod 'MJExtension'
三,注意事项:
属性名需和后台返回的key一致
四,示例:
1,字典转模型:
ModelName *model = [ModelName mj_objectWithKeyValues:response.data];
2,模型转字典:
NSDictionary *parameters = [model mj_keyValues];
3,字典转模型数组:
NSArray * items = [ModelName mj_objectArrayWithKeyValuesArray:response.data];
4,模型转json字符串
[model mj_JSONString];
5,模型数组转json字符串
NSMutableArray *arrSelect = [NSMutableArray array];
for (ModelClass *model in self.modelsArr) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[model mj_keyValues]];
[arrSelect addObject:dic];
}
NSString *jsonStr = [arrSelect mj_JSONString];
6,属性转换名称(比如后台返回的字段为id,转换为大写ID)
.h文件里:
@property (nonatomic, copy) NSString *ID;
.m文件里:
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{@"ID" : @"id"};
}
7,模型里的数组属性转化为其他模型(后台数据字典里包含字典数组)
.h文件里
//parameter: 后台返回数据的key值
@property (nonatomic, strong) NSArray <OtherModelName*> *parameter;
.m文件里
+ (NSDictionary *)mj_objectClassInArray{
return @{@"parameter": [OtherModelName class]};
}
8,既包含其他模型又包含其他模型数组
.h文件里
//parameter: 后台返回数据的key值
@property (nonatomic, strong) OtherModelName *parameterObj;
@property (nonatomic, strong) NSArray <OtherModelName*> *parameter;
.m文件里
+ (NSDictionary *)mj_objectClassInArray{
return @{@"parameter": [OtherModelName class]};
}
网友评论