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

runtime那些事兒(消息機(jī)制)

移動(dòng)開發(fā)
之前在項(xiàng)目中有遇到過用runtime解決改變?nèi)肿煮w的問題,所以再一次感受到了runtime黑魔法的強(qiáng)大,趁現(xiàn)在有機(jī)會(huì)分享一下對(duì)runtime的一些理解。在對(duì)象調(diào)用方法是Objective-C中經(jīng)常使用的功能,也就是消息的傳遞,而Objective-C是C的超集,所以和C不同的是,Objective-C使用的是動(dòng)態(tài)綁定,也就是runtime。Objective-C的消息傳遞和消息機(jī)制也就不多說了,今天主要說的是動(dòng)態(tài)方法,也就是函數(shù)的調(diào)用。

[[163324]]

一、關(guān)于runtime

之前在項(xiàng)目中有遇到過用runtime解決改變?nèi)肿煮w的問題,所以再一次感受到了runtime黑魔法的強(qiáng)大,趁現(xiàn)在有機(jī)會(huì)分享一下對(duì)runtime的一些理解。在對(duì)象調(diào)用方法是Objective-C中經(jīng)常使用的功能,也就是消息的傳遞,而Objective-C是C的超集,所以和C不同的是,Objective-C使用的是動(dòng)態(tài)綁定,也就是runtime。Objective-C的消息傳遞和消息機(jī)制也就不多說了,今天主要說的是動(dòng)態(tài)方法,也就是函數(shù)的調(diào)用。

二、相關(guān)的幾個(gè)函數(shù)

下面一張圖詳細(xì)的概括了每個(gè)函數(shù)調(diào)用的先后以及執(zhí)行的前提

 

消息傳遞函數(shù)的調(diào)用

1.對(duì)象在收到無法解讀的消息后,首先會(huì)調(diào)用所屬類的

  1. + (BOOL)resolveInstanceMethod:(SEL)sel 

這個(gè)方法在運(yùn)行時(shí),沒有找到SEL的IML時(shí)就會(huì)執(zhí)行。這個(gè)函數(shù)是給類利用class_addMethod添加函數(shù)的機(jī)會(huì)。根據(jù)文檔,如果實(shí)現(xiàn)了添加函數(shù)代碼則返回YES,未實(shí)現(xiàn)返回NO。舉個(gè)例子,新建了一個(gè)工程,首先我在ViewController這個(gè)類中執(zhí)行doSomething1這個(gè)方法,代碼如下

  1. // 
  2. //  ViewController.m 
  3. //  RuntimeTest1 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年 www.igancao.com  All rights reserved. 
  7. // 
  8.   
  9. #import "ViewController.h" 
  10.   
  11. @interface ViewController () 
  12.   
  13. @end 
  14.   
  15. @implementation ViewController 
  16.   
  17. - (void)viewDidLoad { 
  18.     [super viewDidLoad]; 
  19.     [self performSelector:@selector(doSomething)]; 
  20.   
  21. - (void)didReceiveMemoryWarning { 
  22.     [super didReceiveMemoryWarning]; 
  23.     // Dispose of any resources that can be recreated. 
  24.   
  25. @end 

運(yùn)行結(jié)果

  1. **2015-12-24 10:35:37.726 RuntimeTest1[1877:337842] -[ViewController doSomething]: unrecognized selector sent to instance 0x7fe9f3736680** 
  2. **2015-12-24 10:35:37.729 RuntimeTest1[1877:337842] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController doSomething]: unrecognized selector sent to instance 0x7fe9f3736680'** 
  3. ***** First throw call stack:** 

不出意外,程序崩潰,因?yàn)闆]有找到doSomething這個(gè)方法,下面我們?cè)诶锩鎸?shí)現(xiàn) + (BOOL)resolveInstanceMethod:(SEL)sel這個(gè)方法,并且判斷如果SEL 是doSomething那就輸出add method here

  1. // 
  2. //  ViewController.m 
  3. //  RuntimeTest1 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年 www.igancao.com All rights reserved. 
  7. // 
  8.   
  9. #import "ViewController.h" 
  10.   
  11. @interface ViewController () 
  12.   
  13. @end 
  14.   
  15. @implementation ViewController 
  16.   
  17. - (void)viewDidLoad { 
  18.     [super viewDidLoad]; 
  19.     [self performSelector:@selector(doSomething)]; 
  20.   
  21. + (BOOL)resolveInstanceMethod:(SEL)sel { 
  22.     if (sel == @selector(doSomething)) { 
  23.         NSLog(@"add method here"); 
  24.         return YES; 
  25.     } 
  26.     return [super resolveInstanceMethod:sel]; 
  27.   
  28. - (void)didReceiveMemoryWarning { 
  29.     [super didReceiveMemoryWarning]; 
  30.     // Dispose of any resources that can be recreated. 
  31.   
  32. @end 

繼續(xù)運(yùn)行,然后看到log

  1. **2015-12-24 10:47:24.687 RuntimeTest1[2007:382077] add method here** 
  2. **2015-12-24 10:47:24.687 RuntimeTest1[2007:382077] -[ViewController doSomething]: unrecognized selector sent to instance 0x7f9568c331f0** 
  3. **2015-12-24 10:47:24.690 RuntimeTest1[2007:382077] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController doSomething]: unrecognized selector sent to instance 0x7f9568c331f0'** 
  4. ***** First throw call stack:** 

可以看到程序依然是崩潰了,但是我們可以看到輸出了add method here,這說明我們 + (BOOL)resolveInstanceMethod:(SEL)sel這個(gè)方法執(zhí)行了,并進(jìn)入了判斷,所以,在這兒,我們可以做一下操作,使這個(gè)方法得到相應(yīng),不至于走到***- (void)doesNotRecognizeSelector:(SEL)aSelector這個(gè)方法中而崩掉了,接下來,我么繼續(xù)操作,如下

  1. // 
  2. //  ViewController.m 
  3. //  RuntimeTest1 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年 www.igancao.com All rights reserved. 
  7. // 
  8.   
  9. #import "ViewController.h" 
  10.   
  11. #import [objc/runtime.h](因識(shí)別問題,此處將尖括號(hào)改為方括號(hào)) 
  12.   
  13. @interface ViewController () 
  14.   
  15. @end 
  16.   
  17. @implementation ViewController 
  18.   
  19. - (void)viewDidLoad { 
  20.     [super viewDidLoad]; 
  21.     [self performSelector:@selector(doSomething)]; 
  22.   
  23. + (BOOL)resolveInstanceMethod:(SEL)sel { 
  24.     if (sel == @selector(doSomething)) { 
  25.         NSLog(@"add method here"); 
  26.         class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "v@:"); 
  27.         return YES; 
  28.     } 
  29.     return [super resolveInstanceMethod:sel]; 
  30. void dynamicMethodIMP (id self, SEL _cmd) { 
  31.     NSLog(@"doSomething SEL"); 
  32.   
  33. - (void)didReceiveMemoryWarning { 
  34.     [super didReceiveMemoryWarning]; 
  35.     // Dispose of any resources that can be recreated. 
  36.   
  37. @end 

導(dǎo)入了并且在+ (BOOL)resolveInstanceMethod:(SEL)sel中執(zhí)行了class_addMethod這個(gè)方法,然后定義了一個(gè)void dynamicMethodIMP (id self, SEL _cmd)這個(gè)函數(shù),運(yùn)行工程,看log

  1. **2015-12-24 11:45:11.934 RuntimeTest1[2284:478571] add method here** 
  2. **2015-12-24 11:45:11.934 RuntimeTest1[2284:478571] doSomething SEL** 

這時(shí)候我們發(fā)現(xiàn),程序并沒有崩潰,而且還輸出了doSomething SEL,這時(shí)候就說明我們已經(jīng)通過runtime成功的向我們這個(gè)類中添加了一個(gè)方法。關(guān)于class_addMethod這個(gè)方法,是這樣定義的

  • cls   在這個(gè)類中添加方法,也就是方法所添加的類

  • name  方法名,這個(gè)可以隨便起的

  • imp   實(shí)現(xiàn)這個(gè)方法的函數(shù)

  • types 定義該數(shù)返回值類型和參數(shù)類型的字符串,這里比如"v@:",其中v就是void,帶表返回類型就是空,@代表參數(shù),這里指的是id(self),這里:指的是方法SEL(_cmd),比如我再定義一個(gè)函數(shù)

  1. int newMethod (id self, SEL _cmd, NSString *str) { 
  2.   
  3.   return 100

那么添加這個(gè)函數(shù)的方法就應(yīng)該是ass_addMethod([self class], @selector(newMethod), (IMP)newMethod, "i@:@");

2.如果在+ (BOOL)resolveInstanceMethod:(SEL)sel中沒有找到或者添加方法

消息繼續(xù)往下傳遞到- (id)forwardingTargetForSelector:(SEL)aSelector看看是不是有對(duì)象可以執(zhí)行這個(gè)方法,我們來重新建個(gè)工程,然后新建一個(gè)叫SecondViewController的類,里面有一個(gè)- (void)secondVCMethod方法,如下

  1. // 
  2. //  SecondViewController.m 
  3. //  RuntimeTest2 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年  www.igancao.com All rights reserved. 
  7. // 
  8.   
  9. #import "SecondViewController.h" 
  10.   
  11. @interface SecondViewController () 
  12.   
  13. @end 
  14.   
  15. @implementation SecondViewController 
  16.   
  17. - (void)viewDidLoad { 
  18.     [super viewDidLoad]; 
  19.     // Do any additional setup after loading the view. 
  20.   
  21. - (void)secondVCMethod { 
  22.     NSLog(@"This is secondVC method !"); 
  23.   
  24. - (void)didReceiveMemoryWarning { 
  25.     [super didReceiveMemoryWarning]; 
  26.     // Dispose of any resources that can be recreated. 
  27.   
  28. /* 
  29. #pragma mark - Navigation 
  30.   
  31. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  32. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  33.     // Get the new view controller using [segue destinationViewController]. 
  34.     // Pass the selected object to the new view controller. 
  35. } 
  36. */ 
  37.   
  38. @end 

工程結(jié)構(gòu)應(yīng)該是這樣的

工程目錄圖

現(xiàn)在我想在ViewController中調(diào)用- (void)secondVCMethod這個(gè)方法,我們知道ViewController和SecondViewController并無繼承關(guān)系,按照正常的步驟去做程序肯定會(huì)因?yàn)樵赩iewController找不到- (void)secondVCMethod這個(gè)方法而直接崩潰的

  1. // 
  2. //  ViewController.m 
  3. //  RuntimeTest2 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年 www.igancao.com  All rights reserved. 
  7. // 
  8.   
  9. #import "ViewController.h" 
  10. #import @interface ViewController () 
  11.   
  12. @end 
  13.   
  14. @implementation ViewController 
  15.   
  16. - (void)viewDidLoad { 
  17.     [super viewDidLoad]; 
  18.      [self performSelector:@selector(secondVCMethod)]; 
  19.   
  20. - (void)didReceiveMemoryWarning { 
  21.     [super didReceiveMemoryWarning]; 
  22.     // Dispose of any resources that can be recreated. 
  23.   
  24. @end 

運(yùn)行結(jié)果

  1. **2015-12-24 13:54:44.314 RuntimeTest2[3164:835814] -[ViewController secondVCMethod]: unrecognized selector sent to instance 0x7fc3a8535c10** 
  2. **2015-12-24 13:54:44.317 RuntimeTest2[3164:835814] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController secondVCMethod]: unrecognized selector sent to instance 0x7fc3a8535c10'** 
  3. ***** First throw call stack:** 

現(xiàn)在我們來處理一下這個(gè)消息,如下

  1. // 
  2. //  ViewController.m 
  3. //  RuntimeTest2 
  4. // 
  5. //  Created by HenryCheng on 15/12/24. 
  6. //  Copyright ?(版權(quán)符號(hào)) 2015年 www.igancao.com All rights reserved. 
  7. // 
  8.   
  9. #import "ViewController.h" 
  10. #import @interface ViewController () 
  11.   
  12. @end 
  13.   
  14. @implementation ViewController 
  15.   
  16. - (void)viewDidLoad { 
  17.     [super viewDidLoad]; 
  18.      [self performSelector:@selector(secondVCMethod)]; 
  19.   
  20. - (id)forwardingTargetForSelector:(SEL)aSelector { 
  21.     Class class = NSClassFromString(@"SecondViewController"); 
  22.     UIViewController *vc = class.new
  23.     if (aSelector == NSSelectorFromString(@"secondVCMethod")) { 
  24.         NSLog(@"secondVC do this !"); 
  25.         return vc; 
  26.     } 
  27.       
  28.     return nil; 
  29.   
  30. + (BOOL)resolveInstanceMethod:(SEL)sel { 
  31.   
  32.     return [super resolveInstanceMethod:sel]; 
  33.   
  34. - (void)didReceiveMemoryWarning { 
  35.     [super didReceiveMemoryWarning]; 
  36.     // Dispose of any resources that can be recreated. 
  37. @end 

運(yùn)行結(jié)果

  1. **2015-12-24 14:00:34.168 RuntimeTest2[3284:870957] secondVC do this !** 
  2. **2015-12-24 14:00:34.169 RuntimeTest2[3284:870957] This is secondVC method !** 

我們會(huì)發(fā)現(xiàn)- (void)secondVCMethod這個(gè)方法執(zhí)行了,程序也并沒有崩潰,原因就是在這一步

  1. - (id)forwardingTargetForSelector:(SEL)aSelector { 
  2.     Class class = NSClassFromString(@"SecondViewController"); 
  3.     UIViewController *vc = class.new
  4.     if (aSelector == NSSelectorFromString(@"secondVCMethod")) { 
  5.         NSLog(@"secondVC do this !"); 
  6.         return vc; 
  7.     } 
  8.       
  9.     return nil; 

在沒有找到- (void)secondVCMethod這個(gè)方法的時(shí)候,消息繼續(xù)傳遞,直到- (id)forwardingTargetForSelector:(SEL)aSelector,然后我在里面創(chuàng)建了一個(gè)SecondViewController的對(duì)象,并且判斷如過有這個(gè)方法,就返回SecondViewController的對(duì)象。這個(gè)函數(shù)就是消息的轉(zhuǎn)發(fā),在這兒我們成功的把消息傳給了SecondViewController,然后讓它來執(zhí)行,所以就執(zhí)行了那個(gè)方法。同時(shí),也相當(dāng)于完成了一個(gè)多繼承!

三、***一點(diǎn)

當(dāng)然,還有好幾個(gè)函數(shù),在上面那張圖里面已經(jīng)清晰的表達(dá)了,有興趣的可以自己試試,看看消息的傳遞順序到底是怎么樣的。上面提到的這些知識(shí)runtime的冰山一角,runtime黑魔法的強(qiáng)大遠(yuǎn)不止于此,比如方法的調(diào)配(Method Swizzling)等,在項(xiàng)目實(shí)戰(zhàn)中還是很有用的,后面有時(shí)間會(huì)再介紹.

參考

責(zé)任編輯:倪明 來源: CocoaChina
相關(guān)推薦

2022-12-25 10:47:52

2013-12-26 14:23:03

定位系統(tǒng)GPS監(jiān)測(cè)

2021-06-09 13:28:40

密碼安全身份認(rèn)證數(shù)據(jù)安全

2011-02-25 14:35:00

2018-09-26 06:50:19

2021-06-02 08:33:31

TPCTPC-H系統(tǒng)

2022-02-08 17:39:04

MySQL服務(wù)器存儲(chǔ)

2022-02-18 19:24:15

性能優(yōu)化代碼

2019-11-20 10:00:56

開源侵權(quán)版權(quán)

2024-11-18 15:30:53

Linux目錄權(quán)限

2013-09-09 10:54:24

2021-04-29 10:30:58

MySQL數(shù)據(jù)遷移

2021-03-18 09:01:53

軟件開發(fā)軟件選型

2023-04-11 07:34:40

分布式系統(tǒng)算法

2024-08-12 08:41:40

2022-04-08 09:47:55

性能優(yōu)化開發(fā)

2022-05-13 14:36:12

網(wǎng)絡(luò)犯罪網(wǎng)絡(luò)攻擊密碼

2022-10-08 00:02:00

CSS工具系統(tǒng)

2017-05-18 16:30:29

Linux內(nèi)存管理

2010-09-14 11:36:24

上網(wǎng)行為管理網(wǎng)絡(luò)安全網(wǎng)康科技
點(diǎn)贊
收藏

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