一、MJExtension第三方框架
我們在iOS開發過程中,我們常常需要將字典數據(也就是JSON數據)與Model模型之間的轉化,例如網絡請求返回的微博數據、等等,如果我們自己全部手動去創建模型並賦值,都是一些毫無技術含量的代碼,費時費力,而且還可能會賦值出錯,讓我們很頭疼。
MJExtension框架就是為了解決這個問題而設計得第三方開源庫。這個開源庫是之前傳智博客的講師李明傑老師寫的,現在他自己出來做了,我iOS入門都是看李明傑老師的培訓視頻學習的,他講得非常好,我非常喜歡他,他也算是我的老師了,他的作品我還是要學習下的。
提供了以下的一些方法實現:
- 簡單的字典 --> 模型
- JSON字符串 --> 模型
- 復雜的字典 --> 模型 (模型里面包含了模型)
- 復雜的字典 --> 模型 (模型的數組屬性里面又裝着模型)
- 復雜的字典 --> 模型(模型屬性名和字典的key不一樣)
- 字典數組 --> 模型數組
- 模型 --> 字典
- 模型數組 --> 字典數組
- 字典 --> CoreData模型
- 歸檔與解檔NSCoding
- 過濾字典的值
MJExtension框架是利用Obj-C的運行時機制編寫的,現在iOS開發語言往Swift語言發展,我不太清楚Swift語言是否也有這種特性,該框架以后會不會在Swift語言上也發展下去不得而知,不過這個框架很輕量級,非常適合初級開發者去看它的源碼,對理解Obj-C的運行時機制有非常大的幫助。
二、Runtime運行時機制簡單了解
Runtime簡稱運行時,就是系統在運行的時候的一些機制,其中最主要的是消息機制。
OC的函數調用類似於消息發送,屬於動態調用過程。在編譯的時候並不能決定真正調用哪個函數。事實證明,在編譯階段,OC可以調用任何函數,即使這個函數並未實現,只要申明過就不會報錯。而C語言在編譯階段就會報錯。只有在真正運行的時候才會根據函數的名稱找到對應的函數來調用。
例如,下面的這個代碼在編譯時會被轉化:
/* OC方法調用 */ [obj makeTest]; /* 編譯時Runtime會將上面的代碼轉為下面的消息發送 */ objc_msgSend(obj, @selector(makeText));
iOS的頂層基類NSObject含有一個指向objc_class結構體的isa指針:
@interface NSObject{
Class isa;
};
typedef struct objc_class *Class; struct objc_class { Class isa; // 指向metaclass,也就是靜態的Class Class super_class ; // 指向其父類 const char *name ; // 類名 long version ; // 類的版本信息,初始化默認為0 /* 一些標識信息,如CLS_CLASS(0x1L)表示該類為普通class; CLS_META(0x2L)表示該類為metaclass */ long info; long instance_size ; // 該類的實例變量大小(包括從父類繼承下來的實例變量 struct objc_ivar_list *ivars; // 用於存儲每個成員變量的地址 /* 與info的一些標志位有關,如是普通class則存儲對象方法,如是metaclass則存儲類方法 */ struct objc_method_list **methodLists ; struct objc_cache *cache; // 指向最近使用的方法的指 針,用於提升效率; struct objc_protocol_list *protocols; // 存儲該類 遵守的協議 };

在objc_msgSend函數的調用過程:
- 首先通過obj的isa指針找到obj對應的Class。
- 在Class中先去cache中通過SEL查找對應函數method
- 若cache中未找到,再去methodLists中查找
- 若methodLists中未找到,則進入superClass按前面的步驟進行遞歸查找
- 若找到method,則將method加入到cache中,以方便下次查找,並通過method中的函數指針跳轉到對應的函數中去執行。
- 如果一直查找到NSObject還沒查找到,則會進入消息動態處理流程。
消息動態處理流程:
/* 1\. 時機處理之一,在這個方法中我們可以利用runtime的特性動態添加方法來處理 */ + (BOOL)resolveInstanceMethod:(SEL)sel; /* 2\. 時機處理之二,在這個方法中看代理能不能處理,如果代理對象能處理,則轉接給代理對象 */ - (id)forwardingTargetForSelector:(SEL)aSelector; /* 3\. 消息轉發之一,該方法返回方法簽名,如果返回nil,則轉發流程終止,拋出異常 */ - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector; /* 4\. 消息轉發之二,在該方法中我們可以對調用方法進行重定向 */ - (void)forwardInvocation:(NSInvocation *)anInvocation;

所以使用Runtime機制我們就可以動態向類添加方法或屬性:
/* 動態向一個類添加屬性 */ class_addIvar(kclass, "expression", size, alignment, "*"); /* 動態向一個類添加方法 */ class_addMethod(kclass, @selector(setExpressionFormula:), (IMP)setExpressionFormula, "v@:@"); class_addMethod(kclass, @selector(getExpressionFormula), (IMP)getExpressionFormula, "@@:"); static void setExpressionFormula(id self, SEL cmd, id value){ NSLog(@"call setExpressionFormula"); } static id getExpressionFormula(id self, SEL cmd) { NSLog(@"call getExpressionFormula"); return nil; }
- v表示void,@表示id類型,:表示SEL類型
- "v@:@":表示返回值為void,接受一個id類型、一個SEL類型、一個id類型的方法
- "@@:":表示返回值為id類型,接受一個id類型和一個SEL類型參數的方法
具體Runtime運行時使用細節,這里就不細講,只是簡單了解下Runtime是可以做到動態向類添加屬性和方法就行。
二、MJExtension使用
MJExtension的大部分方法實現都集成到了分類上,不需要使用新的類,只需要包含頭文件MJExtension.h即可。MJExtension在github上的使用說明已經寫得十分明白了。
1. 簡單的字典 --> 模型
模型類User定義:
typedef enum { SexMale, SexFemale } Sex; @interface User : NSObject @property (copy, nonatomic) NSString *name;/* 姓名 */ @property (copy, nonatomic) NSString *icon;/* 頭像 */ @property (assign, nonatomic) unsigned int age;/* 年齡 */ @property (copy, nonatomic) NSString *height;/* 身高 */ @property (strong, nonatomic) NSNumber *money;/* 資產 */ @property (assign, nonatomic) Sex sex;/* 性別 */ @property (assign, nonatomic, getter=isGay) BOOL gay;/* 是否是同性戀 */ @end
使用實例:
NSDictionary *dict = @{
@"name" : @"Jack", @"icon" : @"lufy.png", @"age" : @20, @"height" : @"1.55", @"money" : @100.9, @"sex" : @(SexFemale),/* 枚舉需要使用NSNumber包裝 */ @"gay" : @"NO" }; //字典轉模型,使用的是mj_objectWithKeyValues:方法 User *user = [User mj_objectWithKeyValues:dict];
2. JSON字符串 --> 模型
使用實例:
// 定義一個JSON字符串 NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}"; // JSON字符串轉模型 User *user = [User mj_objectWithKeyValues:jsonString];
3. 復雜的字典 --> 模型 (模型里面包含了模型)
模型類Status定義:
@interface Status : NSObject @property (copy, nonatomic) NSString *text; @property (strong, nonatomic) User *user;/* 其他模型類型 */ @property (strong, nonatomic) Status *retweetedStatus;/* 自我模型類型 */ @end
使用實例:
NSDictionary *dict = @{ @"text" : @"Agree!Nice weather!", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @"retweetedStatus" : @{ @"text" : @"Nice weather!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } } }; //字典轉模型,模型里面含有模型 Status *status = [Status mj_objectWithKeyValues:dict]; NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon); // text=Agree!Nice weather!, name=Jack, icon=lufy.png NSString *text2 = status.retweetedStatus.text; NSString *name2 = status.retweetedStatus.user.name; NSString *icon2 = status.retweetedStatus.user.icon; NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2); // text2=Nice weather!, name2=Rose, icon2=nami.png
4. 復雜的字典 --> 模型 (模型的數組屬性里面又裝着模型)
模型類Ad和StatusResult定義:
@interface Ad : NSObject @property (copy, nonatomic) NSString *image; @property (copy, nonatomic) NSString *url; @end @interface StatusResult : NSObject /** 數組中存儲模型Status類型數據 */ @property (strong, nonatomic) NSMutableArray *statuses; /** 數組中存儲模型Ad類型數據 */ @property (strong, nonatomic) NSArray *ads; @property (strong, nonatomic) NSNumber *totalNumber; @end #import "MJExtension.h" /* 數組中存儲模型數據,需要說明數組中存儲的模型數據類型 */ @implementation StatusResult /* 實現該方法,說明數組中存儲的模型數據類型 */ + (NSDictionary *)mj_ objectClassInArray{ return @{ @"statuses" : @"Status", @"ads" : @"Ad" }; } @end
使用實例:
NSDictionary *dict = @{ @"text" : @"Agree!Nice weather!", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @"retweetedStatus" : @{ @"text" : @"Nice weather!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } } }; //字典轉模型,模型里面含有模型 Status *status = [Status mj_objectWithKeyValues:dict]; NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon); // text=Agree!Nice weather!, name=Jack, icon=lufy.png NSString *text2 = status.retweetedStatus.text; NSString *name2 = status.retweetedStatus.user.name; NSString *icon2 = status.retweetedStatus.user.icon; NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2); // text2=Nice weather!, name2=Rose, icon2=nami.png
5. 復雜的字典 --> 模型(模型屬性名和字典的key不一樣)
模型類Bag和Student定義:
@interface Bag : NSObject @property (copy, nonatomic) NSString *name; @property (assign, nonatomic) double price; @end @interface Student : 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 #import "MJExtension.h" @implementation /* 設置模型屬性名和字典key之間的映射關系 */ + (NSDictionary *)mj_replacedKeyFromPropertyName{ /* 返回的字典,key為模型屬性名,value為轉化的字典的多級key */ return @{ @"ID" : @"id", @"desc" : @"desciption", @"oldName" : @"name.oldName", @"nowName" : @"name.newName", @"nameChangedTime" : @"name.info[1].nameChangedTime", @"bag" : @"other.bag" }; } @end
使用實例:
NSDictionary *dict = @{ @"id" : @"20", @"desciption" : @"kids", @"name" : @{ @"newName" : @"lufy", @"oldName" : @"kitty", @"info" : @[ @"test-data", @{ @"nameChangedTime" : @"2013-08" } ] }, @"other" : @{ @"bag" : @{ @"name" : @"a red bag", @"price" : @100.7 } } }; //字典轉模型,支持多級映射 Student *stu = [Student mj_objectWithKeyValues:dict]; //打印 NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime); // ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08 NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price); // bagName=a red bag, bagPrice=100.700000
6. 字典數組 --> 模型數組
使用實例:
NSArray *dictArray = @[ @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @{ @"name" : @"Rose", @"icon" : @"nami.png" } ]; //字典數組轉模型數組,使用的是mj_objectArrayWithKeyValuesArray:方法 NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray]; //打印 for (User *user in userArray) { NSLog(@"name=%@, icon=%@", user.name, user.icon); } // name=Jack, icon=lufy.png // name=Rose, icon=nami.png
7. 模型 --> 字典
使用實例:
//創建一個模型對象 User *user = [[User alloc] init]; user.name = @"Jack"; user.icon = @"lufy.png"; Status *status = [[Status alloc] init]; status.user = user; status.text = @"Nice mood!"; //模型轉字典,使用的是mj_keyValues屬性 NSDictionary *statusDict = status.mj_keyValues; NSLog(@"%@", statusDict); /* { text = "Nice mood!"; user = { icon = "lufy.png"; name = Jack; }; } */
8. 模型數組 --> 字典數組
使用實例:
//創建模型數組 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]; //模型數組轉字典數組,使用的是mj_keyValuesArrayWithObjectArray:方法 NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray]; NSLog(@"%@", dictArray); /* ( { icon = "lufy.png"; name = Jack; }, { icon = "nami.png"; name = Rose; } ) */
9. 字典 --> CoreData模型
使用實例:
NSDictionary *dict = @{ @"name" : @"Jack", @"icon" : @"lufy.png", @"age" : @20, @"height" : @1.55, @"money" : @"100.9", @"sex" : @(SexFemale), @"gay" : @"true" }; //字典轉為CoreData模型 NSManagedObjectContext *context = nil; User *user = [User mj_objectWithKeyValues:dict context:context]; [context save:nil];
10. 歸檔與解檔NSCoding
模型類Bag添加實現
@interface Bag : NSObject <NSCoding> @property (copy, nonatomic) NSString *name; @property (assign, nonatomic) double price; @end #import "MJExtension.h" @implementation Bag //添加了下面的宏定義 MJExtensionCodingImplementation /* 實現下面的方法,說明哪些屬性不需要歸檔和解檔 */ + (NSArray *)mj_ignoredCodingPropertyNames{ return @[@"name"]; } @end
使用實例:
//創建模型 Bag *bag = [[Bag alloc] init]; bag.name = @"Red bag"; bag.price = 200.8; //獲取歸檔路徑 NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"]; //歸檔 [NSKeyedArchiver archiveRootObject:bag toFile:file]; //解檔 Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file]; NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price); // name=(null), price=200.800000
11. 過濾字典的值
模型類Book實現:
@interface Book: NSObject @property (copy, nonatomic) NSString *name; @property (strong, nonatomic) NSDate *publishedTime; @end #import "MJExtension.h" @implementation Book /* 轉化過程中對字典的值進行過濾和進一步轉化 */ - (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property { if ([property.name isEqualToString:@"publisher"]) { if (oldValue == nil) { return @""; } } else if (property.type.typeClass == [NSDate class]) { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; return [fmt dateFromString:oldValue]; } return oldValue; } @end
使用實例:
NSDictionary *dict = @{ @"name" : @"5分鍾突破iOS開發", @"publishedTime" : @"2011-09-10" }; //字典轉模型,過濾name為nil的情況,把NSString轉為NSDate Book *book = [Book mj_objectWithKeyValues:dict]; //打印 NSLog(@"name=%@, publishedTime=%@", book.name, book.publishedTime);
MJExtension的github地址點這里:CoderMJLee/MJExtension
作者:貝勒老爺
鏈接:https://www.jianshu.com/p/53ff148c6670
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。