自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)中runtime常用的幾種方法

移動開發(fā)
公司項(xiàng)目中用了一些 runtime 相關(guān)的知識, 初看時(shí)有些蒙, 雖然用的并不多, 但還是想著系統(tǒng)的把 runtime 相關(guān)的常用方法整理一下, 自己以后用著方便, 也希望對看到的朋友有所幫助。

公司項(xiàng)目中用了一些 runtime 相關(guān)的知識, 初看時(shí)有些蒙, 雖然用的并不多, 但還是想著系統(tǒng)的把 runtime 相關(guān)的常用方法整理一下, 自己以后用著方便, 也希望對看到的朋友有所幫助。

iOS開發(fā)中runtime常用的幾種方法

一、runtime 簡介

runtime 簡稱運(yùn)行時(shí),是系統(tǒng)在運(yùn)行的時(shí)候的一些機(jī)制,其中最主要的是消息機(jī)制。它是一套比較底層的純 C 語言 API, 屬于一個(gè) C 語言庫,包含了很多底層的 C 語言 API。我們平時(shí)編寫的 OC 代碼,在程序運(yùn)行過程時(shí),其實(shí)最終都是轉(zhuǎn)成了 runtime 的 C 語言代碼。如下所示:

  1. // OC代碼: 
  2. [Person coding]; 
  3.  
  4. //運(yùn)行時(shí) runtime 會將它轉(zhuǎn)化成 C 語言的代碼: 
  5. objc_msgSend(Person, @selector(coding)); 

二、相關(guān)函數(shù)

  1. // 遍歷某個(gè)類所有的成員變量 
  2. class_copyIvarList 
  3.  
  4. // 遍歷某個(gè)類所有的方法 
  5. class_copyMethodList 
  6.  
  7. // 獲取指定名稱的成員變量 
  8. class_getInstanceVariable 
  9.  
  10. // 獲取成員變量名 
  11. ivar_getName 
  12.  
  13. // 獲取成員變量類型編碼 
  14. ivar_getTypeEncoding 
  15.  
  16. // 獲取某個(gè)對象成員變量的值 
  17. object_getIvar 
  18.  
  19. // 設(shè)置某個(gè)對象成員變量的值 
  20. object_setIvar 
  21.  
  22. // 給對象發(fā)送消息 
  23. objc_msgSend 

三、相關(guān)應(yīng)用

  • 更改屬性值
  • 動態(tài)添加屬性
  • 動態(tài)添加方法
  • 交換方法的實(shí)現(xiàn)
  • 攔截并替換方法
  • 在方法上增加額外功能
  • 歸檔解檔
  • 字典轉(zhuǎn)模型

以上八種用法用代碼都實(shí)現(xiàn)了, 文末會貼出代碼地址.

 


runtime

四、代碼實(shí)現(xiàn)

要使用runtime,要先引入頭文件#import

4.1 更改屬性值

用 runtime 修改一個(gè)對象的屬性值

  1. unsigned int count = 0; 
  2.   // 動態(tài)獲取類中的所有屬性(包括私有) 
  3.   Ivar *ivar = class_copyIvarList(_person.class, &count); 
  4.   // 遍歷屬性找到對應(yīng)字段 
  5.   for (int i = 0; i < count; i ++) { 
  6.       Ivar tempIvar = ivar[i]; 
  7.       const char *varChar = ivar_getName(tempIvar); 
  8.       NSString *varString = [NSString stringWithUTF8String:varChar]; 
  9.       if ([varString isEqualToString:@"_name"]) { 
  10.           // 修改對應(yīng)的字段值 
  11.           object_setIvar(_person, tempIvar, @"更改屬性值成功"); 
  12.           break; 
  13.       } 
  14.   } 

4.2 動態(tài)添加屬性

用 runtime 為一個(gè)類添加屬性, iOS 分類里一般會這樣用, 我們建立一個(gè)分類, NSObject+NNAddAttribute.h, 并添加以下代碼:

  1. - (void)setName:(NSString *)name { 
  2.     objc_setAssociatedObject(self, @"name"name, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
  3.  
  4. - (NSString *)name { 
  5.     return objc_getAssociatedObject(self, @"name"); 

這樣只要引用 NSObject+NNAddAttribute.h, 用 NSObject 創(chuàng)建的對象就會有一個(gè) name 屬性, 我們可以直接這樣寫:

  1. NSObject *person = [NSObject new]; 
  2.   person.name = @"以夢為馬"

4.3 動態(tài)添加方法

person 類中沒有 coding 方法,我們用 runtime 給 person 類添加了一個(gè)名字叫 coding 的方法,最終再調(diào)用coding方法做出相應(yīng). 下面代碼的幾個(gè)參數(shù)需要注意一下:

  1. - (void)buttonClick:(UIButton *)sender { 
  2.     /* 
  3.      動態(tài)添加 coding 方法 
  4.      (IMP)codingOC 意思是 codingOC 的地址指針; 
  5.      "v@:" 意思是,v 代表無返回值 void,如果是 i 則代表 int;@代表 id sel; : 代表 SEL _cmd; 
  6.      “v@:@@” 意思是,兩個(gè)參數(shù)的沒有返回值。 
  7.      */ 
  8.     class_addMethod([_person class], @selector(coding), (IMP)codingOC, "v@:"); 
  9.     // 調(diào)用 coding 方法響應(yīng)事件 
  10.     if ([_person respondsToSelector:@selector(coding)]) { 
  11.         [_person performSelector:@selector(coding)]; 
  12.         self.testLabelText = @"添加方法成功"
  13.     } else { 
  14.         self.testLabelText = @"添加方法失敗"
  15.     } 
  16.  
  17. // 編寫 codingOC 的實(shí)現(xiàn) 
  18. void codingOC(id self,SEL _cmd) { 
  19.     NSLog(@"添加方法成功"); 

4.4 交換方法的實(shí)現(xiàn)

某個(gè)類有兩個(gè)方法, 比如 person 類有兩個(gè)方法, coding 方法與 eating 方法, 我們用 runtime 交換一下這兩個(gè)方法, 就會出現(xiàn)這樣的情況, 當(dāng)我們調(diào)用 coding 的時(shí)候, 執(zhí)行的是 eating, 當(dāng)我們調(diào)用 eating 的時(shí)候, 執(zhí)行的是 coding, 如下面的動態(tài)效果圖.

  1. Method oriMethod = class_getInstanceMethod(_person.class, @selector(coding)); 
  2. Method curMethod = class_getInstanceMethod(_person.class, @selector(eating)); 
  3. method_exchangeImplementations(oriMethod, curMethod); 

 


交換方法的實(shí)現(xiàn)

4.5 攔截并替換方法

這個(gè)功能和上面的其實(shí)有些類似, 攔截并替換方法可以攔截并替換同一個(gè)類的, 也可以在兩個(gè)類之間進(jìn)行, 我這里用了兩個(gè)不同的類, 下面是簡單的代碼實(shí)現(xiàn).

  1. _person = [NNPerson new]; 
  2.   _library = [NNLibrary new]; 
  3.   self.testLabelText = [_library libraryMethod]; 
  4.   Method oriMethod = class_getInstanceMethod(_person.class, @selector(changeMethod)); 
  5.   Method curMethod = class_getInstanceMethod(_library.class, @selector(libraryMethod)); 
  6.   method_exchangeImplementations(oriMethod, curMethod); 

4.6 在方法上增加額外功能

這個(gè)使用場景還是挺多的, 比如我們需要記錄 APP 中某一個(gè)按鈕的點(diǎn)擊次數(shù), 這個(gè)時(shí)候我們便可以利用 runtime 來實(shí)現(xiàn)這個(gè)功能. 我這里寫了個(gè) UIButton 的子類, 然后在 + (void)load 中用 runtime 給它增加了一個(gè)功能, 核心代碼及實(shí)現(xiàn)效果圖如下:

  1. + (void)load { 
  2.     static dispatch_once_t onceToken; 
  3.     dispatch_once(&onceToken, ^{ 
  4.         Method oriMethod = class_getInstanceMethod(self.class, @selector(sendAction:to:forEvent:)); 
  5.         Method cusMethod = class_getInstanceMethod(self.class, @selector(customSendAction:to:forEvent:)); 
  6.         // 判斷自定義的方法是否實(shí)現(xiàn), 避免崩潰 
  7.         BOOL addSuccess = class_addMethod(self.class, @selector(sendAction:to:forEvent:), method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod)); 
  8.         if (addSuccess) { 
  9.             // 沒有實(shí)現(xiàn), 將源方法的實(shí)現(xiàn)替換到交換方法的實(shí)現(xiàn) 
  10.             class_replaceMethod(self.class, @selector(customSendAction:to:forEvent:), method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod)); 
  11.         } else { 
  12.             // 已經(jīng)實(shí)現(xiàn), 直接交換方法 
  13.             method_exchangeImplementations(oriMethod, cusMethod); 
  14.         } 
  15.     }); 

 


在方法上增加額外功能

4.7 歸檔解檔

當(dāng)我們使用 NSCoding 進(jìn)行歸檔及解檔時(shí), 如果不用 runtime, 那么不管模型里面有多少屬性, 我們都需要對其實(shí)現(xiàn)一遍 encodeObject 和 decodeObjectForKey 方法, 如果模型里面有 10000 個(gè)屬性, 那么我們就需要寫 10000 句encodeObject 和 decodeObjectForKey 方法, 這個(gè)時(shí)候用 runtime, 便可以充分體驗(yàn)其好處(以下只是核心代碼, 具體代碼請見 demo).

  1. - (void)encodeWithCoder:(NSCoder *)aCoder { 
  2.     unsigned int count = 0; 
  3.     // 獲取類中所有屬性 
  4.     Ivar *ivars = class_copyIvarList(self.class, &count); 
  5.     // 遍歷屬性 
  6.     for (int i = 0; i < count; i ++) { 
  7.         // 取出 i 位置對應(yīng)的屬性 
  8.         Ivar ivar = ivars[i]; 
  9.         // 查看屬性 
  10.         const char *name = ivar_getName(ivar); 
  11.         NSString *key = [NSString stringWithUTF8String:name]; 
  12.         // 利用 KVC 進(jìn)行取值,根據(jù)屬性名稱獲取對應(yīng)的值 
  13.         id value = [self valueForKey:key]; 
  14.         [aCoder encodeObject:value forKey:key]; 
  15.     } 
  16.     free(ivars); 
  17.  
  18. - (instancetype)initWithCoder:(NSCoder *)aDecoder { 
  19.     if (self = [super init]) { 
  20.         unsigned int count = 0; 
  21.         // 獲取類中所有屬性 
  22.         Ivar *ivars = class_copyIvarList(self.class, &count); 
  23.         // 遍歷屬性 
  24.         for (int i = 0; i < count; i ++) { 
  25.             // 取出 i 位置對應(yīng)的屬性 
  26.             Ivar ivar = ivars[i]; 
  27.             // 查看屬性 
  28.             const char *name = ivar_getName(ivar); 
  29.             NSString *key = [NSString stringWithUTF8String:name]; 
  30.             // 進(jìn)行解檔取值 
  31.             id value = [aDecoder decodeObjectForKey:key]; 
  32.             // 利用 KVC 對屬性賦值 
  33.             [self setValue:value forKey:key]; 
  34.         } 
  35.     } 
  36.     return self; 

4.8 字典轉(zhuǎn)模型

字典轉(zhuǎn)模型我們通常用的都是第三方, MJExtension, YYModel 等, 但也有必要了解一下其實(shí)現(xiàn)方式: 遍歷模型中的所有屬性,根據(jù)模型的屬性名,去字典中查找對應(yīng)的 key,取出對應(yīng)的值,給模型的屬性賦值。

  1. /** 字典轉(zhuǎn)模型 **/ 
  2. + (instancetype)modelWithDict:(NSDictionary *)dict { 
  3.     id objc = [[self alloc] init]; 
  4.     unsigned int count = 0; 
  5.     // 獲取成員屬性數(shù)組 
  6.     Ivar *ivarList = class_copyIvarList(self, &count); 
  7.     // 遍歷所有的成員屬性名 
  8.     for (int i = 0; i < count; i ++) { 
  9.         // 獲取成員屬性 
  10.         Ivar ivar = ivarList[i]; 
  11.         // 獲取成員屬性名 
  12.         NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; 
  13.         NSString *key = [ivarName substringFromIndex:1]; 
  14.         // 從字典中取出對應(yīng) value 給模型屬性賦值 
  15.         id value = dict[key]; 
  16.         // 獲取成員屬性類型 
  17.         NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)]; 
  18.         // 判斷 value 是不是字典 
  19.         if ([value isKindOfClass:[NSDictionary class]]) { 
  20.             ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""]; 
  21.             ivarType = [ivarType stringByReplacingOccurrencesOfString:@""" withString:@""]; 
  22.             Class modalClass = NSClassFromString(ivarType); 
  23.             // 字典轉(zhuǎn)模型 
  24.             if (modalClass) { 
  25.                 // 字典轉(zhuǎn)模型 
  26.                 value = [modalClass modelWithDict:value]; 
  27.             } 
  28.         } 
  29.         if ([value isKindOfClass:[NSArray class]]) { 
  30.             // 判斷對應(yīng)類有沒有實(shí)現(xiàn)字典數(shù)組轉(zhuǎn)模型數(shù)組的協(xié)議 
  31.             if ([self respondsToSelector:@selector(arrayContainModelClass)]) { 
  32.                 // 轉(zhuǎn)換成id類型,就能調(diào)用任何對象的方法 
  33.                 id idSelf = self; 
  34.                 // 獲取數(shù)組中字典對應(yīng)的模型 
  35.                 NSString *type = [idSelf arrayContainModelClass][key]; 
  36.                 // 生成模型 
  37.                 Class classModel = NSClassFromString(type); 
  38.                 NSMutableArray *arrM = [NSMutableArray array]; 
  39.                 // 遍歷字典數(shù)組,生成模型數(shù)組 
  40.                 for (NSDictionary *dict in value) { 
  41.                     // 字典轉(zhuǎn)模型 
  42.                     id model =  [classModel modelWithDict:dict]; 
  43.                     [arrM addObject:model]; 
  44.                 } 
  45.                 // 把模型數(shù)組賦值給value 
  46.                 value = arrM; 
  47.             } 
  48.         } 
  49.         // KVC 字典轉(zhuǎn)模型 
  50.         if (value) { 
  51.             [objc setValue:value forKey:key]; 
  52.         } 
  53.     } 
  54.     return objc; 

上面的所有代碼都可以在這里下載: runtime 練習(xí): NNRuntimeTest

https://github.com/liuzhongning/NNLearn/tree/master/002.%20NNRuntimeTest

責(zé)任編輯:未麗燕 來源: 簡書
相關(guān)推薦

2010-05-17 15:17:06

MySQL常用操作

2024-06-03 08:26:34

Android開發(fā)監(jiān)聽器

2021-03-08 09:32:04

Python文件命令

2020-10-16 18:35:53

JavaScript字符串正則表達(dá)式

2009-08-25 09:22:01

DataGridVie

2013-08-21 11:31:21

iPhone圖片方法

2010-06-03 08:55:43

LINQ

2021-06-08 11:42:12

Pandas數(shù)據(jù)分析Python

2009-09-18 12:29:55

2020-01-10 16:23:44

Springboot停止服務(wù)Java

2020-08-24 08:05:47

JavaScriptJavaScript 頁面

2009-08-31 09:19:31

c#隱藏窗口

2011-06-16 10:48:33

session

2009-09-09 11:24:46

PHP實(shí)現(xiàn)MVC

2021-02-26 13:20:48

Shell空行Linux

2024-02-21 08:33:27

GoReadDir性能

2010-04-30 16:22:07

Unix終端

2010-01-22 14:46:25

C++語言

2010-10-26 09:23:03

Web Service

2009-06-11 13:49:30

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號