手把手教你使用MJExtension(簡(jiǎn)單使用)

一、MJExtension第三方框架

我們?cè)趇OS開(kāi)發(fā)過(guò)程中,我們常常需要將字典數(shù)據(jù)(也就是JSON數(shù)據(jù))與Model模型之間的轉(zhuǎn)化,例如網(wǎng)絡(luò)請(qǐng)求返回的微博數(shù)據(jù)、等等,如果我們自己全部手動(dòng)去創(chuàng)建模型并賦值,都是一些毫無(wú)技術(shù)含量的代碼,費(fèi)時(shí)費(fèi)力,而且還可能會(huì)賦值出錯(cuò),讓我們很頭疼。

MJExtension框架就是為了解決這個(gè)問(wèn)題而設(shè)計(jì)得第三方開(kāi)源庫(kù)。這個(gè)開(kāi)源庫(kù)是之前傳智博客的講師李明杰老師寫的,現(xiàn)在他自己出來(lái)做了,我iOS入門都是看李明杰老師的培訓(xùn)視頻學(xué)習(xí)的,他講得非常好,我非常喜歡他,他也算是我的老師了,他的作品我還是要學(xué)習(xí)下的。

提供了以下的一些方法實(shí)現(xiàn):

  1. 簡(jiǎn)單的字典 --> 模型
  2. JSON字符串 --> 模型
  3. 復(fù)雜的字典 --> 模型 (模型里面包含了模型)
  4. 復(fù)雜的字典 --> 模型 (模型的數(shù)組屬性里面又裝著模型)
  5. 復(fù)雜的字典 --> 模型(模型屬性名和字典的key不一樣)
  6. 字典數(shù)組 --> 模型數(shù)組
  7. 模型 --> 字典
  8. 模型數(shù)組 --> 字典數(shù)組
  9. 字典 --> CoreData模型
  10. 歸檔與解檔NSCoding
  11. 過(guò)濾字典的值

MJExtension框架是利用Obj-C的運(yùn)行時(shí)機(jī)制編寫的,現(xiàn)在iOS開(kāi)發(fā)語(yǔ)言往Swift語(yǔ)言發(fā)展,我不太清楚Swift語(yǔ)言是否也有這種特性,該框架以后會(huì)不會(huì)在Swift語(yǔ)言上也發(fā)展下去不得而知,不過(guò)這個(gè)框架很輕量級(jí),非常適合初級(jí)開(kāi)發(fā)者去看它的源碼,對(duì)理解Obj-C的運(yùn)行時(shí)機(jī)制有非常大的幫助。

二、Runtime運(yùn)行時(shí)機(jī)制簡(jiǎn)單了解

Runtime簡(jiǎn)稱運(yùn)行時(shí),就是系統(tǒng)在運(yùn)行的時(shí)候的一些機(jī)制,其中最主要的是消息機(jī)制。

OC的函數(shù)調(diào)用類似于消息發(fā)送,屬于動(dòng)態(tài)調(diào)用過(guò)程。在編譯的時(shí)候并不能決定真正調(diào)用哪個(gè)函數(shù)。事實(shí)證明,在編譯階段,OC可以調(diào)用任何函數(shù),即使這個(gè)函數(shù)并未實(shí)現(xiàn),只要申明過(guò)就不會(huì)報(bào)錯(cuò)。而C語(yǔ)言在編譯階段就會(huì)報(bào)錯(cuò)。只有在真正運(yùn)行的時(shí)候才會(huì)根據(jù)函數(shù)的名稱找到對(duì)應(yīng)的函數(shù)來(lái)調(diào)用。

例如,下面的這個(gè)代碼在編譯時(shí)會(huì)被轉(zhuǎn)化:

/* OC方法調(diào)用 */
[obj makeTest];
/* 編譯時(shí)Runtime會(huì)將上面的代碼轉(zhuǎn)為下面的消息發(fā)送 */
objc_msgSend(obj, @selector(makeText));

iOS的頂層基類NSObject含有一個(gè)指向objc_class結(jié)構(gòu)體的isa指針:

@interface NSObject{
Class isa;
};
typedef struct objc_class *Class;
struct objc_class {
    Class isa; // 指向metaclass,也就是靜態(tài)的Class
    Class super_class ; // 指向其父類
    const char *name ; // 類名
    long version ; // 類的版本信息,初始化默認(rèn)為0
    /* 一些標(biāo)識(shí)信息,如CLS_CLASS(0x1L)表示該類為普通class;
    CLS_META(0x2L)表示該類為metaclass */
    long info; 
    long instance_size ; // 該類的實(shí)例變量大小(包括從父類繼承下來(lái)的實(shí)例變量
    struct objc_ivar_list *ivars; // 用于存儲(chǔ)每個(gè)成員變量的地址
     /* 與info的一些標(biāo)志位有關(guān),如是普通class則存儲(chǔ)對(duì)象方法,如是metaclass則存儲(chǔ)類方法 */
    struct objc_method_list **methodLists ; 
    struct objc_cache *cache; // 指向最近使用的方法的指    針,用于提升效率;
    struct objc_protocol_list *protocols; // 存儲(chǔ)該類   遵守的協(xié)議
    };

1795722-93c68c68674d07ee.jpg

在objc_msgSend函數(shù)的調(diào)用過(guò)程:

  1. 首先通過(guò)obj的isa指針找到obj對(duì)應(yīng)的Class。
  2. 在Class中先去cache中通過(guò)SEL查找對(duì)應(yīng)函數(shù)method
  3. 若cache中未找到,再去methodLists中查找
  4. 若methodLists中未找到,則進(jìn)入superClass按前面的步驟進(jìn)行遞歸查找
  5. 若找到method,則將method加入到cache中,以方便下次查找,并通過(guò)method中的函數(shù)指針跳轉(zhuǎn)到對(duì)應(yīng)的函數(shù)中去執(zhí)行。
  6. 如果一直查找到NSObject還沒(méi)查找到,則會(huì)進(jìn)入消息動(dòng)態(tài)處理流程。

消息動(dòng)態(tài)處理流程:

/* 1\. 時(shí)機(jī)處理之一,在這個(gè)方法中我們可以利用runtime的特性動(dòng)態(tài)添加方法來(lái)處理 */
+ (BOOL)resolveInstanceMethod:(SEL)sel;
/* 2\. 時(shí)機(jī)處理之二,在這個(gè)方法中看代理能不能處理,如果代理對(duì)象能處理,則轉(zhuǎn)接給代理對(duì)象 */
- (id)forwardingTargetForSelector:(SEL)aSelector;
/* 3\. 消息轉(zhuǎn)發(fā)之一,該方法返回方法簽名,如果返回nil,則轉(zhuǎn)發(fā)流程終止,拋出異常 */
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
/* 4\. 消息轉(zhuǎn)發(fā)之二,在該方法中我們可以對(duì)調(diào)用方法進(jìn)行重定向 */
- (void)forwardInvocation:(NSInvocation *)anInvocation;

1795722-008e4e09b0e93dd7.jpg

所以使用Runtime機(jī)制我們就可以動(dòng)態(tài)向類添加方法或?qū)傩裕?/strong>

/* 動(dòng)態(tài)向一個(gè)類添加屬性 */
class_addIvar(kclass, "expression", size, alignment, "*");
/* 動(dòng)態(tài)向一個(gè)類添加方法 */
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;
}

  1. v表示void,@表示id類型,:表示SEL類型
  2. "v@:@":表示返回值為void,接受一個(gè)id類型、一個(gè)SEL類型、一個(gè)id類型的方法
  3. "@@:":表示返回值為id類型,接受一個(gè)id類型和一個(gè)SEL類型參數(shù)的方法

具體Runtime運(yùn)行時(shí)使用細(xì)節(jié),這里就不細(xì)講,只是簡(jiǎn)單了解下Runtime是可以做到動(dòng)態(tài)向類添加屬性和方法就行。

二、MJExtension使用

MJExtension的大部分方法實(shí)現(xiàn)都集成到了分類上,不需要使用新的類,只需要包含頭文件MJExtension.h即可。MJExtension在github上的使用說(shuō)明已經(jīng)寫得十分明白了。

1. 簡(jiǎn)單的字典 --> 模型

模型類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;/* 資產(chǎn) */
@property (assign, nonatomic) Sex sex;/* 性別 */
@property (assign, nonatomic, getter=isGay) BOOL gay;/* 是否是同性戀 */
@end

使用實(shí)例:

NSDictionary *dict = @{
    @"name" : @"Jack",
    @"icon" : @"lufy.png",
    @"age" : @20,
    @"height" : @"1.55",
    @"money" : @100.9,
    @"sex" : @(SexFemale),/* 枚舉需要使用NSNumber包裝 */
    @"gay" : @"NO"
};
//字典轉(zhuǎn)模型,使用的是mj_objectWithKeyValues:方法
User *user = [User mj_objectWithKeyValues:dict];

2. JSON字符串 --> 模型

使用實(shí)例:

// 定義一個(gè)JSON字符串
NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
// JSON字符串轉(zhuǎn)模型
User *user = [User mj_objectWithKeyValues:jsonString];

3. 復(fù)雜的字典 --> 模型 (模型里面包含了模型)

模型類Status定義:

@interface Status : NSObject
@property (copy, nonatomic) NSString *text;
@property (strong, nonatomic) User *user;/* 其他模型類型 */
@property (strong, nonatomic) Status *retweetedStatus;/* 自我模型類型 */
@end

使用實(shí)例:

NSDictionary *dict = @{
    @"text" : @"Agree!Nice weather!",
    @"user" : @{
        @"name" : @"Jack",
        @"icon" : @"lufy.png"
    },
    @"retweetedStatus" : @{
        @"text" : @"Nice weather!",
        @"user" : @{
            @"name" : @"Rose",
            @"icon" : @"nami.png"
        }
    }
};
//字典轉(zhuǎn)模型,模型里面含有模型
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. 復(fù)雜的字典 --> 模型 (模型的數(shù)組屬性里面又裝著模型)

模型類Ad和StatusResult定義:

@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end

@interface StatusResult : NSObject
/** 數(shù)組中存儲(chǔ)模型Status類型數(shù)據(jù) */
@property (strong, nonatomic) NSMutableArray *statuses;
/** 數(shù)組中存儲(chǔ)模型Ad類型數(shù)據(jù) */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end

#import "MJExtension.h"
/* 數(shù)組中存儲(chǔ)模型數(shù)據(jù),需要說(shuō)明數(shù)組中存儲(chǔ)的模型數(shù)據(jù)類型 */
@implementation StatusResult
/* 實(shí)現(xiàn)該方法,說(shuō)明數(shù)組中存儲(chǔ)的模型數(shù)據(jù)類型 */
+ (NSDictionary *)mj_ objectClassInArray{
    return @{ @"statuses" : @"Status",
              @"ads" : @"Ad"
            };
}
@end

使用實(shí)例:

NSDictionary *dict = @{
    @"text" : @"Agree!Nice weather!",
    @"user" : @{
        @"name" : @"Jack",
        @"icon" : @"lufy.png"
    },
    @"retweetedStatus" : @{
        @"text" : @"Nice weather!",
        @"user" : @{
            @"name" : @"Rose",
            @"icon" : @"nami.png"
        }
    }
};
//字典轉(zhuǎn)模型,模型里面含有模型
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. 復(fù)雜的字典 --> 模型(模型屬性名和字典的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 
/* 設(shè)置模型屬性名和字典key之間的映射關(guān)系 */
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    /* 返回的字典,key為模型屬性名,value為轉(zhuǎn)化的字典的多級(jí)key */
    return @{
               @"ID" : @"id",
               @"desc" : @"desciption",
               @"oldName" : @"name.oldName",
               @"nowName" : @"name.newName",
               @"nameChangedTime" : @"name.info[1].nameChangedTime",
               @"bag" : @"other.bag"
           };
}
@end

使用實(shí)例:

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
        }
    }
};
//字典轉(zhuǎn)模型,支持多級(jí)映射
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. 字典數(shù)組 --> 模型數(shù)組

使用實(shí)例:

NSArray *dictArray = @[
                         @{
                             @"name" : @"Jack",
                             @"icon" : @"lufy.png"
                         },
                         @{
                             @"name" : @"Rose",
                             @"icon" : @"nami.png"
                         }
                     ];
//字典數(shù)組轉(zhuǎn)模型數(shù)組,使用的是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. 模型 --> 字典

使用實(shí)例:

//創(chuàng)建一個(gè)模型對(duì)象
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";
//模型轉(zhuǎn)字典,使用的是mj_keyValues屬性
NSDictionary *statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);
/*
 {
 text = "Nice mood!";
 user =     {
 icon = "lufy.png";
 name = Jack;
 };
 }
 */

8. 模型數(shù)組 --> 字典數(shù)組

使用實(shí)例:

//創(chuàng)建模型數(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ù)組,使用的是mj_keyValuesArrayWithObjectArray:方法
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
/*
 (
 {
 icon = "lufy.png";
 name = Jack;
 },
 {
 icon = "nami.png";
 name = Rose;
 }
 )
 */

9. 字典 --> CoreData模型

使用實(shí)例:

NSDictionary *dict = @{
                         @"name" : @"Jack",
                         @"icon" : @"lufy.png",
                         @"age" : @20,
                         @"height" : @1.55,
                         @"money" : @"100.9",
                         @"sex" : @(SexFemale),
                         @"gay" : @"true"
                     };
//字典轉(zhuǎn)為CoreData模型
NSManagedObjectContext *context = nil;
User *user = [User mj_objectWithKeyValues:dict 
                                  context:context];
[context save:nil];

10. 歸檔與解檔NSCoding

模型類Bag添加實(shí)現(xiàn)

@interface Bag : NSObject <NSCoding>
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end

#import "MJExtension.h"
@implementation Bag
//添加了下面的宏定義
MJExtensionCodingImplementation

/* 實(shí)現(xiàn)下面的方法,說(shuō)明哪些屬性不需要?dú)w檔和解檔 */
+ (NSArray *)mj_ignoredCodingPropertyNames{
    return @[@"name"];
}
@end

使用實(shí)例:

//創(chuàng)建模型
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. 過(guò)濾字典的值

模型類Book實(shí)現(xiàn):

@interface Book: NSObject
@property (copy, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *publishedTime;
@end

#import "MJExtension.h"
@implementation Book
/* 轉(zhuǎn)化過(guò)程中對(duì)字典的值進(jìn)行過(guò)濾和進(jìn)一步轉(zhuǎn)化 */
- (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

使用實(shí)例:

NSDictionary *dict = @{
                       @"name" : @"5分鐘突破iOS開(kāi)發(fā)",
                       @"publishedTime" : @"2011-09-10"
                       };
//字典轉(zhuǎn)模型,過(guò)濾name為nil的情況,把NSString轉(zhuǎn)為NSDate
Book *book = [Book mj_objectWithKeyValues:dict];
//打印
NSLog(@"name=%@, publishedTime=%@", book.name, book.publishedTime);

MJExtension的github地址點(diǎn)這里:CoderMJLee/MJExtension

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

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

  • 對(duì)于從事 iOS 開(kāi)發(fā)人員來(lái)說(shuō),所有的人都會(huì)答出【runtime 是運(yùn)行時(shí)】什么情況下用runtime?大部分人能...
    夢(mèng)夜繁星閱讀 3,812評(píng)論 7 64
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,083評(píng)論 0 9
  • 引導(dǎo) 對(duì)于從事 iOS 開(kāi)發(fā)人員來(lái)說(shuō),所有的人都會(huì)答出「 Runtime 是運(yùn)行時(shí) 」,什么情況下用 Runtim...
    Winny_園球閱讀 4,326評(píng)論 3 75
  • 引導(dǎo) 對(duì)于從事 iOS 開(kāi)發(fā)人員來(lái)說(shuō),所有的人都會(huì)答出「 Runtime 是運(yùn)行時(shí) 」,什么情況下用 Runtim...
    Winny_園球閱讀 403評(píng)論 0 0
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,681評(píng)論 1 32

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