MJExtension
關(guān)鍵方法
- 轉(zhuǎn)模型:[ZLUser mj_objectWithKeyValues:<#(id)#>]
- MJExtensionLogAllProperties這個(gè)在模型內(nèi)打印模型內(nèi)全部屬性
- 模型內(nèi)含有另外的模型數(shù)組(注意是類(lèi)方法)
+ (NSDictionary *)mj_objectClassInArray {
return @{
@"statuses" : [ZLStatus class],
@"ads" : @"MJAd"
};
}
- 模型內(nèi)替換屬性(注意是類(lèi)方法)和多級(jí)映射
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info[1].nameChangedTime",
@"bag" : @"other.bag"
};
}
轉(zhuǎn)模型數(shù)組:[MJUser mj_objectArrayWithKeyValuesArray:dictArray];
模型轉(zhuǎn)字典
//1. 新建模型給模型屬性賦值
MJUser *user = [[MJUser alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
MJStatus *status = [[MJStatus alloc] init];
status.user = user;
status.text = @"今天的心情不錯(cuò)埋哟!";
// 2.將模型轉(zhuǎn)為字典
NSDictionary *statusDict = status.mj_keyValues;
// 3.新建多級(jí)映射的模型
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 = @"小書(shū)包";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.mj_keyValues;
- 模型數(shù)組轉(zhuǎn)字典
// 1.新建模型數(shù)組
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.將模型數(shù)組轉(zhuǎn)為字典數(shù)組
NSArray *dictArray = [MJUser mj_keyValuesArrayWithObjectArray:userArray];
- 在具體使用中替換屬性(不在每個(gè)模型內(nèi)部轉(zhuǎn),在相對(duì)應(yīng)的字典轉(zhuǎn)模型的地方更換屬性名和字典key不一樣的地方)
//1. 先寫(xiě)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. 再字典轉(zhuǎn)模型
ZLStudent *stu = [ZLStudent mj_objectWithKeyValues:dict4];
- 在具體使用中字典內(nèi)包含字典轉(zhuǎn)模型內(nèi)嵌套模型
NSDictionary *dict3 = @{
@"statuses" : @[
@{
@"text" : @"今天天氣真不錯(cuò)谜诫!",
@"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數(shù)組測(cè)試
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);
}
-自帶歸結(jié)當(dāng)方案
// 創(chuàng)建模型
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用法詳解
字典轉(zhuǎn)模型,json轉(zhuǎn)模型
ZLUser *user = [ZLUser yy_modelWithJSON:json];
ZLUser *user = [ZLUser yy_modelWithJSON:dict1];
ZLUser *user = [ZLUser yy_modelWithDictionary:dict1];模型轉(zhuǎn)json
NSDictionary *json = [user yy_modelToJSONObject];模型內(nèi)替換屬性(注意是類(lèi)方法)和多級(jí)映射,注意相比如MJExtensionYYModel可以一對(duì)多映射
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id",@"ID",@"book_id"]};
}
json轉(zhuǎn)模型中:如果一個(gè)屬性對(duì)應(yīng)了多個(gè)json key,那么轉(zhuǎn)換過(guò)程會(huì)按順序查找急膀,并使用第一個(gè)不為空的值画株。
- 模型內(nèi)含有另外的模型數(shù)組(注意是類(lèi)方法)
// 返回容器類(lèi)中的所需要存放的數(shù)據(jù)類(lèi)型 (以 Class 或 Class Name 的形式)辆飘。
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{
@"statuses" : [ZLStatus class],
@"ads" : @"MJAd"
};
}
- 字典數(shù)組轉(zhuǎn)模型數(shù)組
NSArray *userArray = [NSArray yy_modelArrayWithClass:[ZLUser class] json:dictArray1];
總結(jié)發(fā)現(xiàn)YYModel處理速度更快用法相對(duì)來(lái)說(shuō)少一些,YYModel其中有一個(gè)多級(jí)映射MJExtension沒(méi)有,MJExtension可以處理的情況多點(diǎn).都是很好的框架,核心原理是利用運(yùn)行時(shí)動(dòng)態(tài)獲取屬性列表然后給屬性賦值,并且做出一些錯(cuò)誤處理.