oc runtime 动态调用方法
首先文章是参考: https://www.jianshu.com/p/0280e95bebeb
根据方法名获取方法对应的SEL类型数据,
再根据SEL获取方法指针IMP
最后根据IMP定义方法,运行。
调用无参方法:
SEL selector = NSSelectorFromString(@"方法名");
IMP imp = [self methodForSelector:selector];
void (*func)(id, SEL) = (void *)imp;
func(self, selector);
调用有参和有返回值的方法:
SEL selector = NSSelectorFromString(@"方法名");
IMP imp = [self methodForSelector:selector];
id (*func)(id, SEL,NSString *, NSArray *) = (void *)imp;
id ret = func(self, selector,@"1",@[@"1",@"2"]);
如果是调用实例方法:
SEL selector = NSSelectorFromString(@"方法名");
IMP imp = [self instanceMethodForSelector:selector];
id (*func)(id, SEL,NSString *, NSArray *) = (void *)imp;
id ret = func(self, selector,@"1",@[@"1",@"2"]);