MJExtension使用指導(dǎo)(轉(zhuǎn))

當(dāng)自己看到原文的排版時(shí),真的是。。。,自己花了點(diǎn)時(shí)間重新排版了下,分享出來!

原文鏈接:IOS 字典模型互轉(zhuǎn)框架 MJExtension

MJExtension能做什么?

MJExtension是一套字典和模型之間互相轉(zhuǎn)換的超輕量級框架

MJExtension能完成的功能

字典(JSON) --> 模型(Model) 模型(Model) --> 字典(JSON) 字典數(shù)組(JSONArray) --> 模型數(shù)組(ModelArray) 模型數(shù)組(ModelArray) --> 字典數(shù)組(JSONArray)

詳盡用法主要參考 main.m中的各個(gè)函數(shù) 以及 NSObject+MJKeyValue.h

MJExtension和JSONModel、Mantle等框架的區(qū)別

1. 轉(zhuǎn)換速率:

最近一次測試表明:MJExtension > JSONModel > Mantle

各位開發(fā)者也可以自行測試

2.具體用法:

JSONModel

要求所有模型類必須繼承自JSONModel基類

Mantle

要求所有模型類必須繼承自MTModel基類

MJExtension

不需要你的模型類繼承任何特殊基類,毫無污染,毫無侵入性

如何使用MJExtension

方法一:cocoapods導(dǎo)入pod 'MJExtension'

方法二:手動導(dǎo)入

將MJExtensionExample/MJExtensionExample/MJExtension文件夾中的所有源代碼拽入項(xiàng)目中導(dǎo)入主頭文件:#import"MJExtension.h"MJExtension.hMJConst.hMJConst.mMJFoundation.hMJFoundation.mMJIvar.hMJIvar.mMJType.hMJType.mNSObject+MJCoding.hNSObject+MJCoding.mNSObject+MJIvar.hNSObject+MJIvar.mNSObject+MJKeyValue.hNSObject+MJKeyValue.m

1.最簡單的字典轉(zhuǎn)模型

typedefenum{? ? SexMale,? ? ? ? SexFemale} Sex;@interfaceUser:NSObject@property(copy,nonatomic)NSString*name;@property(copy,nonatomic)NSString*icon;@property(assign,nonatomic)intage;@property(assign,nonatomic)doubleheight;@property(strong,nonatomic)NSNumber*money;@property(assign,nonatomic) Sex sex;@endNSDictionary*dict = @{@"name":@"Jack",@"icon":@"lufy.png",@"age": @20,@"height":@"1.55",@"money": @100.9,@"sex": @(SexFemale)? ? ? ? ? ? };// 將字典轉(zhuǎn)為User模型User *user = [User objectWithKeyValues:dict];NSLog(@"name=%@, icon=%@, age=%d, height=%@, money=%@, sex=%d", user.name, user.icon, user.age, user.height, user.money, user.sex);// name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1

核心代碼1:

[User objectWithKeyValues:dict]

2.模型中嵌套模型

@interfaceStatus:NSObject/** 微博文本內(nèi)容 */@property(copy,nonatomic)NSString*text;/** 微博作者 */@property(strong,nonatomic) User *user;/** 轉(zhuǎn)發(fā)的微博 */@property(strong,nonatomic) Status *retweetedStatus;@endNSDictionary*dict = @{@"text":@"是啊,今天天氣確實(shí)不錯!",@"user": @{@"name":@"Jack",@"icon":@"lufy.png"},@"retweetedStatus": @{@"text":@"今天天氣真不錯!",@"user": @{@"name":@"Rose",@"icon":@"nami.png"}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? };// 將字典轉(zhuǎn)為Status模型Status *status = [Status objectWithKeyValues:dict];NSString*text = status.text;NSString*name = status.user.name;NSString*icon = status.user.icon;NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);// text=是啊,今天天氣確實(shí)不錯!, name=Jack, icon=lufy.pngNSString*text2 = status.retweetedStatus.text;NSString*name2 = status.retweetedStatus.user.name;NSString*icon2 = status.retweetedStatus.user.icon;NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);// text2=今天天氣真不錯!, name2=Rose, icon2=nami.png

核心代碼2

[Status objectWithKeyValues:dict]

3.模型中有個(gè)數(shù)組屬性,數(shù)組里面又要裝著其它模型

@interfaceAd:NSObject@property(copy,nonatomic)NSString*image;@property(copy,nonatomic)NSString*url;@end@interfaceStatusResult:NSObject/** 存放著一堆的微博數(shù)據(jù)(里面都是Status模型) */@property(strong,nonatomic)NSMutableArray*statuses;/** 存放著一堆的廣告數(shù)據(jù)(里面都是Ad模型) */@property(strong,nonatomic)NSArray*ads;@property(strong,nonatomic)NSNumber*totalNumber;@end@implementationStatusResult// 實(shí)現(xiàn)這個(gè)方法的目的:告訴MJExtension框架statuses和ads數(shù)組里面裝的是什么模型/*? ? + (NSDictionary *)objectClassInArray{

return @{

@"statuses" : [Status class],

@"ads" : [Ad class]? ? };

}

+ (Class)objectClassInArray:(NSString *)propertyName{

if ([propertyName isEqualToString:@"statuses"]) {

return [Status class];

} else if ([propertyName isEqualToString:@"ads"]) {

return [Ad class];? ? }

return nil;}

*/// 這個(gè)方法對比上面的2個(gè)方法更加沒有侵入性和污染,因?yàn)椴恍枰獙?dǎo)入Status和Ad的頭文件+ (NSDictionary*)objectClassInArray{return@{@"statuses":@"Status",@"ads":@"Ad"};}@endNSDictionary*dict = @{@"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"};// 將字典轉(zhuǎn)為StatusResult模型StatusResult *result = [StatusResult objectWithKeyValues:dict];NSLog(@"totalNumber=%@", result.totalNumber);// totalNumber=2014// 打印statuses數(shù)組中的模型屬性for(Status *statusinresult.statuses) {NSString*text = status.text;NSString*name = status.user.name;NSString*icon = status.user.icon;NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);}// text=今天天氣真不錯!, name=Rose, icon=nami.png// text=明天去旅游了, name=Jack, icon=lufy.png// 打印ads數(shù)組中的模型屬性for(Ad *adinresult.ads) {NSLog(@"image=%@, url=%@", ad.image, ad.url);}// image=ad01.png, url=http://www.ad01.com// image=ad02.png, url=http://www.ad02.com

核心代碼3:

在模型內(nèi)部實(shí)現(xiàn)+ (NSDictionary*)objectClassInArray方法

[StatusResult objectWithKeyValues:dict]

4.模型中的屬性名和字典中的key不相同(或者需要多級映射)

@interfaceBag:NSObject@property(copy,nonatomic)NSString*name;@property(assign,nonatomic)doubleprice;@end@interfaceStudent:NSObject@property(copy,nonatomic)NSString*ID;@property(copy,nonatomic)NSString*desc;@property(copy,nonatomic)NSString*nowName;@property(copy,nonatomic)NSString*oldName;@property(copy,nonatomic)NSString*nameChangedTime;@property(strong,nonatomic) Bag *bag;@end@implementationStudent// 實(shí)現(xiàn)這個(gè)方法的目的:告訴MJExtension框架模型中的屬性名對應(yīng)著字典的哪個(gè)key+ (NSDictionary*)replacedKeyFromPropertyName{return@{@"ID":@"id",@"desc":@"desciption",@"oldName":@"name.oldName",@"nowName":@"name.newName",@"nameChangedTime":@"name.info.nameChangedTime",@"bag":@"other.bag"};}@endNSDictionary*dict = @{@"id":@"20",@"desciption":@"孩子",@"name": @{@"newName":@"lufy",@"oldName":@"kitty",@"info": @{@"nameChangedTime":@"2013-08"}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },@"other": @{@"bag": @{@"name":@"小書包",@"price": @100.7}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? };// 將字典轉(zhuǎn)為Student模型Student *stu = [Student objectWithKeyValues:dict];// 打印Student模型的屬性NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);// ID=20, desc=孩子, oldName=kitty, nowName=lufy, nameChangedTime=2013-08NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);// bagName=小書包, bagPrice=100.700000

核心代碼4:

在模型內(nèi)部實(shí)現(xiàn)+ (NSDictionary*)replacedKeyFromPropertyName方法

[Student objectWithKeyValues:dict]

5.將一個(gè)字典數(shù)組轉(zhuǎn)成模型數(shù)組

NSArray*dictArray = @[? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @{@"name":@"Jack",@"icon":@"lufy.png",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @{@"name":@"Rose",@"icon":@"nami.png",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ];// 將字典數(shù)組轉(zhuǎn)為User模型數(shù)組NSArray*userArray = [User objectArrayWithKeyValuesArray:dictArray];// 打印userArray數(shù)組中的User模型屬性for(User *userinuserArray) {NSLog(@"name=%@, icon=%@", user.name, user.icon);}// name=Jack, icon=lufy.png// name=Rose, icon=nami.png

核心代碼5:

[User objectArrayWithKeyValuesArray:dictArray]

6.將一個(gè)模型轉(zhuǎn)成字典

// 新建模型User *user = [[User alloc] init];user.name=@"Jack";user.icon=@"lufy.png";Status *status = [[Status alloc] init];status.user= user;status.text=@"今天的心情不錯!";// 將模型轉(zhuǎn)為字典NSDictionary*statusDict = status.keyValues;NSLog(@"%@", statusDict);/*{ text = "今天的心情不錯!";

user = {

icon = "lufy.png";

name = Jack;

};

}*/// 多級映射的模型Student *stu = [[Student alloc] init];stu.ID=@"123";stu.oldName=@"rose";stu.nowName=@"jack";stu.desc=@"handsome";stu.nameChangedTime=@"2018-09-08";Bag *bag = [[Bag alloc] init];bag.name=@"小書包";bag.price=205;stu.bag= bag;NSDictionary*stuDict = stu.keyValues;NSLog(@"%@", stuDict);/*

{? ? desciption = handsome;

id = 123;

name = {

info ={

nameChangedTime = "2018-09-08";

};

newName = jack;

oldName = rose;

};

other = {

bag ={

name = "小書包";

price = 205;

};

};

}

*/

核心代碼6:

status.keyValues、stu.keyValues

7.將一個(gè)模型數(shù)組轉(zhuǎn)成字典數(shù)組

// 新建模型數(shù)組User *user1 = [[User alloc] init];user1.name=@"Jack";user1.icon=@"lufy.png";User *user2 = [[User alloc] init];user2.name=@"Rose";user2.icon=@"nami.png";NSArray*userArray = @[user1, user2];// 將模型數(shù)組轉(zhuǎn)為字典數(shù)組NSArray*dictArray = [User keyValuesArrayWithObjectArray:userArray];NSLog(@"%@", dictArray);/*(

{? ? ? ? icon = "lufy.png";? ? ? ? name = Jack;? ? },

{? ? ? ? icon = "nami.png";? ? ? ? name = Rose;? ? }? )*/

核心代碼7:

[User keyValuesArrayWithObjectArray:userArray]

更多用法

參考NSObject+MJKeyValue.h參考NSObject+MJCoding.h

文/PP_Abner(簡書作者)

原文鏈接:http://www.itdecent.cn/p/93c242452b9b

著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容