objc_msgSend

首先要引入頭文件: ios

#import <objc/runtime.h> 函數

#import <objc/message.h> spa


平時咱們調用一個函數以下: io

-(void)test{ function

NSLog(@"test"); test

import

會使用以下: select

[self test]; ios8

換成runtime時,以下: 方法

objc_msgSend(self, @selector(test));


當有多個參數時,如:

-(void)test:(NSString *)arg1  arg2:(NSString *)arg2{

}

以下:

[self test:arg1 arg2:arg2];

objc_msgSend(self, @selector(test), arg1, arg2);

或者(由於selector找不到對應的方法,因此要用下面這種):

SEL testFunc = NSSelectorFromString(@"test:arg2:");

objc_msgSend(self, testFunc, arg1, arg2);



最近在ios8時,發現以下報錯:

Too many arguments to function call, expected 0, have 3


解決方法:

((void(*)(id,SEL, id,id))objc_msgSend)(self, testFunc, arg1, arg2);

本站公眾號
   歡迎關注本站公眾號,獲取更多信息