Runtime: 運行時機制
OC語言就是運行時機制, 在編譯時不檢測調用那個方法, 在運行時進行調用方法
( 一 ) Runtime之消息發送機制
底層都是實現了[p performSelector:@selector(eat)];
Person * p = [[Person alloc]init];
[p performSelector:@selector(eat)];
- 第一步 導入頭文件
#import <objc/message.h>
- 第二步 使用objc_msgSend消息機制
Target - BulidSetting - 搜索msg - 設置為NO
/*** 使用消息機制 ***/
objc_msgSend(p, @selector(eat));
objc_msgSend(p, @selector(run:),10);
- 運行時調用類方法
/*** 調用類方法 ***/
Class personClass = [Person class];
[[Person class] performSelector:@selector(eat)];
[personClass performSelector:@selector(eat)];
objc_msgSend(personClass, @selector(eat));
( 二 ) Runtime之交換方法
- 第一步 導入頭文件
#import <objc/message.h>
- 實現交換方法
/*** 交換方法機制 ***/
method_exchangeImplementations(<#Method m1#>, <#Method m2#>)
- 獲取類方法和對象方法
//1. 獲取對象方法
class_getInstanceMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>)
//2. 獲取類方法
class_getClassMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>)
- 實現交換方法
Method imageNamedMethod = class_getClassMethod([UIImage class], @selector(imageNamed:));
Method MY_imageNamedMethod = class_getClassMethod([UIImage class], @selector(MY_imageNamed:));
//實現交換方法
method_exchangeImplementations(imageNamedMethod, MY_imageNamedMethod);
五 Runtime之動態添加方法
- 動態加載方法
//1. 動態加載類方法
+(BOOL)resolveClassMethod:(SEL)sel
//2. 動態加載對象方法
+(BOOL)resolveInstanceMethod:(SEL)sel
//1. 動態加載無參方法
[p performSelector:@selector(eat)];
//2. 動態加載有參方法
[p performSelector:@selector(eat:) withObject:@123];
- 動態加載對象方法
VC中動態調用沒有定義的eat方法
Person * p = [[Person alloc]init];
[p performSelector:@selector(eat)];
Person.m中實現動態加載
#import "Person.h"
#import <objc/message.h>
@implementation Person
////1. 動態加載類方法
//+(BOOL)resolveClassMethod:(SEL)sel
////2. 動態加載對象方法
//+(BOOL)resolveInstanceMethod:(SEL)sel
#pragma mark - BOOL)resolveInstanceMethod:(SEL)sel
//1. 動態添加方法, 首先要實現這個resolveInstanceMethod方法
//2. resolveInstanceMethod調用: 當一個方法沒有實現, 又被調用了, 就會調用reolve
//3. resolveInstanceMethod作用: 就知道了哪些方法沒有被調用, 從而動態添加該方法
//4. sel: 被調用了 但沒有被實現的方法
+(BOOL)resolveInstanceMethod:(SEL)sel{
if (sel == @selector(eat)) {
NSLog(@"%@",NSStringFromSelector(@selector(eat)));
//動態添加eat方法
/***
__unsafe_unretained Class cls: 給哪個類添加方法
SEL name : 添加方法的名字編號
IMP imp : 方法實現, 函數入口(C), 函數名, (IMP)強轉
const char *types : 方法類型: [void: v , id: @ , sel: : ]
***/
class_addMethod(self, sel, (IMP)eatAction, "v@:");
//處理完
return YES;
}
return YES;
}
//定義C語言函數方法
//隱式參數: 沒有返回值, 參數(id SEL)
//void(id, SEL)
void eatAction(id self, SEL _cmd){
NSLog(@"調用eat%@ %@",self, NSStringFromSelector(_cmd));
}