這個(gè)是我寫(xiě)的一個(gè)demo夹抗,你們可以去看看俊马,歡迎star
https://github.com/mafangchao/MCChangeLabelFont-OC/tree/master
一鹅颊、關(guān)于runtime
之前在項(xiàng)目中有遇到過(guò)用runtime解決改變?nèi)肿煮w的問(wèn)題敷存,所以再一次感受到了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ī)制也就不多說(shuō)了禽拔,今天主要說(shuō)的是動(dòng)態(tài)方法,也就是函數(shù)的調(diào)用室叉。
二睹栖、相關(guān)的幾個(gè)函數(shù)
消息傳遞函數(shù)的調(diào)用
1.對(duì)象在收到無(wú)法解讀的消息后,首先會(huì)調(diào)用所屬類的
- (BOOL)resolveInstanceMethod:(SEL)sel
這個(gè)方法在運(yùn)行時(shí)茧痕,沒(méi)有找到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è)方法,代碼如下
import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(doSomething)];
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
運(yùn)行結(jié)果
2015-12-24 10:35:37.726 RuntimeTest1[1877:337842] -[ViewController doSomething]: unrecognized selector sent to instance 0x7fe9f3736680
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'
***** First throw call stack:**
不出意外气破,程序崩潰聊浅,因?yàn)闆](méi)有找到doSomething這個(gè)方法,下面我們?cè)诶锩鎸?shí)現(xiàn) + (BOOL)resolveInstanceMethod:(SEL)sel這個(gè)方法,并且判斷如果SEL 是doSomething那就輸出add method here
import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(doSomething)];
}
- (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(doSomething)) {
NSLog(@"add method here");
return YES;
}
return [super resolveInstanceMethod:sel];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
繼續(xù)運(yùn)行低匙,然后看到log
2015-12-24 10:47:24.687 RuntimeTest1[2007:382077] add method here
2015-12-24 10:47:24.687 RuntimeTest1[2007:382077] -[ViewController doSomething]: unrecognized selector sent to instance 0x7f9568c331f0
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'
***** First throw call stack:**
可以看到程序依然是崩潰了旷痕,但是我們可以看到輸出了add method here,這說(shuō)明我們 + (BOOL)resolveInstanceMethod:(SEL)sel這個(gè)方法執(zhí)行了努咐,并進(jìn)入了判斷苦蒿,所以,在這兒渗稍,我們可以做一下操作佩迟,使這個(gè)方法得到相應(yīng),不至于走到最后- (void)doesNotRecognizeSelector:(SEL)aSelector這個(gè)方法中而崩掉了竿屹,接下來(lái)报强,我么繼續(xù)操作,如下
import "ViewController.h"
import objc/runtime.h
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(doSomething)];
}
- (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(doSomething)) {
NSLog(@"add method here");
class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "v@:");
return YES;
}
return [super resolveInstanceMethod:sel];
}
void dynamicMethodIMP (id self, SEL _cmd) {
NSLog(@"doSomething SEL");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
導(dǎo)入了并且在+ (BOOL)resolveInstanceMethod:(SEL)sel中執(zhí)行了class_addMethod這個(gè)方法拱燃,然后定義了一個(gè)void dynamicMethodIMP (id self, SEL _cmd)這個(gè)函數(shù)秉溉,運(yùn)行工程,看log
2015-12-24 11:45:11.934 RuntimeTest1[2284:478571] add method here
2015-12-24 11:45:11.934 RuntimeTest1[2284:478571] doSomething SEL
這時(shí)候我們發(fā)現(xiàn)碗誉,程序并沒(méi)有崩潰召嘶,而且還輸出了doSomething SEL,這時(shí)候就說(shuō)明我們已經(jīng)通過(guò)runtime成功的向我們這個(gè)類中添加了一個(gè)方法哮缺。關(guān)于class_addMethod這個(gè)方法弄跌,是這樣定義的
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
cls 在這個(gè)類中添加方法袍冷,也就是方法所添加的類
name 方法名算谈,這個(gè)可以隨便起的
imp 實(shí)現(xiàn)這個(gè)方法的函數(shù)
types 定義該數(shù)返回值類型和參數(shù)類型的字符串闽坡,這里比如"v@:"吊趾,其中v就是void,帶表返回類型就是空躬贡,@代表參數(shù)抵拘,這里指的是id(self)祖凫,這里:指的是方法SEL(_cmd)非竿,比如我再定義一個(gè)函數(shù)
int newMethod (id self, SEL _cmd, NSString *str) {
return 100;
}
那么添加這個(gè)函數(shù)的方法就應(yīng)該是ass_addMethod([self class], @selector(newMethod), (IMP)newMethod, "i@:@");
2.如果在+ (BOOL)resolveInstanceMethod:(SEL)sel中沒(méi)有找到或者添加方法
消息繼續(xù)往下傳遞到- (id)forwardingTargetForSelector:(SEL)aSelector看看是不是有對(duì)象可以執(zhí)行這個(gè)方法蜕着,我們來(lái)重新建個(gè)工程,然后新建一個(gè)叫SecondViewController的類红柱,里面有一個(gè)- (void)secondVCMethod方法侮东,如下
import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}(void)secondVCMethod {
NSLog(@"This is secondVC method !");
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
現(xiàn)在我想在ViewController中調(diào)用- (void)secondVCMethod這個(gè)方法,我們知道ViewController和SecondViewController并無(wú)繼承關(guān)系豹芯,按照正常的步驟去做程序肯定會(huì)因?yàn)樵赩iewController找不到- (void)secondVCMethod這個(gè)方法而直接崩潰的
import "ViewController.h"
import @interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(secondVCMethod)];
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
運(yùn)行結(jié)果
2015-12-24 13:54:44.314 RuntimeTest2[3164:835814] -[ViewController secondVCMethod]: unrecognized selector sent to instance 0x7fc3a8535c10
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'
***** First throw call stack:**
現(xiàn)在我們來(lái)處理一下這個(gè)消息,如下
import "ViewController.h"
import @interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(secondVCMethod)];
}-
(id)forwardingTargetForSelector:(SEL)aSelector {
Class class = NSClassFromString(@"SecondViewController");
UIViewController *vc = class.new;
if (aSelector == NSSelectorFromString(@"secondVCMethod")) {
NSLog(@"secondVC do this !");
return vc;
}return nil;
}
-
(BOOL)resolveInstanceMethod:(SEL)sel {
return [super resolveInstanceMethod:sel];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
運(yùn)行結(jié)果
2015-12-24 14:00:34.168 RuntimeTest2[3284:870957] secondVC do this !
2015-12-24 14:00:34.169 RuntimeTest2[3284:870957] This is secondVC method !
我們會(huì)發(fā)現(xiàn)- (void)secondVCMethod這個(gè)方法執(zhí)行了驱敲,程序也并沒(méi)有崩潰铁蹈,原因就是在這一步-
(id)forwardingTargetForSelector:(SEL)aSelector {
Class class = NSClassFromString(@"SecondViewController");
UIViewController *vc = class.new;
if (aSelector == NSSelectorFromString(@"secondVCMethod")) {
NSLog(@"secondVC do this !");
return vc;
}return nil;
}
在沒(méi)有找到- (void)secondVCMethod這個(gè)方法的時(shí)候,消息繼續(xù)傳遞,直到- (id)forwardingTargetForSelector:(SEL)aSelector握牧,然后我在里面創(chuàng)建了一個(gè)SecondViewController的對(duì)象容诬,并且判斷如過(guò)有這個(gè)方法,就返回SecondViewController的對(duì)象沿腰。這個(gè)函數(shù)就是消息的轉(zhuǎn)發(fā)览徒,在這兒我們成功的把消息傳給了SecondViewController,然后讓它來(lái)執(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ì)再介紹.