美文网首页
Runtime之objc_msgSend的使用方法

Runtime之objc_msgSend的使用方法

作者: 嗯o哼 | 来源:发表于2020-09-10 21:00 被阅读0次

一、需要引入头文件

#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

相关文章

网友评论

      本文标题:Runtime之objc_msgSend的使用方法

      本文链接:https://www.haomeiwen.com/subject/exurkktx.html