MJExtension
关键方法
- 转模型:[ZLUser mj_objectWithKeyValues:<#(id)#>]
- MJExtensionLogAllProperties这个在模型内打印模型内全部属性
- 模型内含有另外的模型数组(注意是类方法)
+ (NSDictionary *)mj_objectClassInArray {
return @{
@"statuses" : [ZLStatus class],
@"ads" : @"MJAd"
};
}
- 模型内替换属性(注意是类方法)和多级映射
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info[1].nameChangedTime",
@"bag" : @"other.bag"
};
}
转模型数组:[MJUser mj_objectArrayWithKeyValuesArray:dictArray];
模型转字典
//1. 新建模型给模型属性赋值
MJUser *user = [[MJUser alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
MJStatus *status = [[MJStatus alloc] init];
status.user = user;
status.text = @"今天的心情不错!";
// 2.将模型转为字典
NSDictionary *statusDict = status.mj_keyValues;
// 3.新建多级映射的模型
MJStudent *stu = [[MJStudent alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08";
stu.books = @[@"Good book", @"Red book"];
MJBag *bag = [[MJBag alloc] init];
bag.name = @"小书包";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.mj_keyValues;
- 模型数组转字典
// 1.新建模型数组
MJUser *user1 = [[MJUser alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";
MJUser *user2 = [[MJUser alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";
NSArray *userArray = @[user1, user2];
// 2.将模型数组转为字典数组
NSArray *dictArray = [MJUser mj_keyValuesArrayWithObjectArray:userArray];
- 在具体使用中替换属性(不在每个模型内部转,在相对应的字典转模型的地方更换属性名和字典key不一样的地方)
//1. 先写setupReplace方法替换属性
[ZLStudent mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info[1].nameChangedTime",
@"bag" : @"other.bag"
};
}];
//2. 再字典转模型
ZLStudent *stu = [ZLStudent mj_objectWithKeyValues:dict4];
- 在具体使用中字典内包含字典转模型内嵌套模型
NSDictionary *dict3 = @{
@"statuses" : @[
@{
@"text" : @"今天天气真不错!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
},
@{
@"text" : @"明天去旅游了",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
}
],
@"ads" : @[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.小码哥ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.小码哥ad02.com"
}
],
@"totalNumber" : @"2014",
@"previousCursor" : @"13476589",
@"nextCursor" : @"13476599"
};
//核心代码
[ZLStatusResult mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : [ZLStatus class], // @"statuses" : [MJStatus class],
@"ads" : @"MJAd" // @"ads" : [MJAd class]
};
}];
// 打印statuses数组测试
for (ZLStatus *status in statusResult.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
MJExtensionLog(@"text=%@, name=%@, icon=%@", text, name, icon);
}
NSLog(@"statusResult == %@",statusResult);
for (MJAd *ad in statusResult.ads) {
NSLog(@"%@---%@",ad.image,ad.url);
}
-自带归结当方案
// 创建模型
MJBag *bag = [[MJBag alloc] init];
bag.name = @"Red bag";
bag.price = 200.8;
NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"bag.data"];
// 归档
[NSKeyedArchiver archiveRootObject:bag toFile:file];
// 解档
MJBag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
MJExtensionLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
YYModel
YYModel用法详解
字典转模型,json转模型
ZLUser *user = [ZLUser yy_modelWithJSON:json];
ZLUser *user = [ZLUser yy_modelWithJSON:dict1];
ZLUser *user = [ZLUser yy_modelWithDictionary:dict1];模型转json
NSDictionary *json = [user yy_modelToJSONObject];模型内替换属性(注意是类方法)和多级映射,注意相比如MJExtensionYYModel可以一对多映射
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id",@"ID",@"book_id"]};
}
json转模型中:如果一个属性对应了多个json key,那么转换过程会按顺序查找,并使用第一个不为空的值。
- 模型内含有另外的模型数组(注意是类方法)
// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{
@"statuses" : [ZLStatus class],
@"ads" : @"MJAd"
};
}
- 字典数组转模型数组
NSArray *userArray = [NSArray yy_modelArrayWithClass:[ZLUser class] json:dictArray1];
总结发现YYModel处理速度更快用法相对来说少一些,YYModel其中有一个多级映射MJExtension没有,MJExtension可以处理的情况多点.都是很好的框架,核心原理是利用运行时动态获取属性列表然后给属性赋值,并且做出一些错误处理.