如何调用我之前使用以下代码保存的方法:
SEL sel = @selector(someMethod:param:);
Method myMethod = class_getInstanceMethod([SomeClass class], sel);
正如您可能想象的那样,调用[SomeClass someMethod]
不会起作用,因为后来我混合了原始方法。
如何调用我之前使用以下代码保存的方法:
SEL sel = @selector(someMethod:param:);
Method myMethod = class_getInstanceMethod([SomeClass class], sel);
正如您可能想象的那样,调用[SomeClass someMethod]
不会起作用,因为后来我混合了原始方法。
您需要将指针类型转换为正确的函数类型,记住方法有两个隐式参数,self 和 _cmd。来自 Apple 的运行时文档:
void (*setter)(id, SEL, BOOL);
int i;
setter = (void (*)(id, SEL, BOOL))[target methodForSelector:@selector(setFilled:)];
for ( i = 0; i < 1000, i++ )
setter(targetList[i], @selector(setFilled:), YES);
(编辑)
请记住,Method 类型是一个结构,并且在 ObjC2 运行时中,它是不透明的,因此您无法直接访问其成员 - 您需要使用它method_getImplementation(myMethod)
来获取可以像上面那样进行类型转换的 IMP。