开发者

How can I call a Method that I saved using class_getInstanceMethod from Objective-C?

How do I call a method that I previously saved using the code below:开发者_如何学Go

SEL sel = @selector(someMethod:param:);
Method myMethod = class_getInstanceMethod([SomeClass class], sel);

As you may imagine, calling [SomeClass someMethod] is not going to work because, later, I swizzle the original method.


You need to typecast the pointer to the proper function type, keeping in mind that methods have two implicit arguments, self and _cmd. From Apple's runtime docs:

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);

(Edit)

Keep in mind that the Method type is a struct, and in the ObjC2 runtime, it's opaque, so you don't have direct access to its members - you'll need to use method_getImplementation(myMethod) to get an IMP that you can typecast like above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜