Runtime之objc_msgSend的使用方法

2020-09-10  本文已影响0人  嗯o哼

一、需要引入头文件

#import <objc/message.h>

二、进行类型转换

Person *person = [[Person alloc] init];
/// 向person 对象发送一条post方法 参数为2
((void(*)(id,SEL,int))objc_msgSend)(person,@selector(post:),2);
其中(void(*)(id,SEL,int) 表示
返回类型为void 参数为id,方法名,参数类型

三、返回类型

void(*) 是无返回值
如果需要返回字符串或者int 等其他类型可以写成
NSString * (*) / int(*) 等等

Person *person = [[Person alloc] init];
 NSString *name = ((NSString *(*)(id,SEL,NSString *)) objc_msgSend)(person,@selector(getName:),@"lee");
 NSLog(@"%@",name);

打印结果为 lee

上一篇 下一篇

猜你喜欢

热点阅读