Objective-C和SEL/IMP的使用

Objective-C和SEL/IMP的使用

问题描述:

我的另一个有关优化Objective C程序的问题启发了以下问题:有人举一个简短的例子吗?当Method具有两个(或更多)整数作为输入时使用SEL和IMP?

Another question of mine about optimizing Objective C programs inspired the following: does anyone have a short example using SEL and IMP when theMethod has two (or more) integers for input?

这是一个

Here's a good tutorial for getting the current IMP (with an overview of IMPs). A very basic example of IMPs and SELs is:

- (void)methodWithInt:(int)firstInt andInt:(int)secondInt { NSLog(@"%d", firstInt + secondInt); }

SEL theSelector = @selector(methodWithInt:andInt:);
IMP theImplementation = [self methodForSelector:theSelector]; 
//note that if the method doesn't return void, you have to explicitly typecast the IMP, e.g. int(* foo)(id, SEL, int, int) = ...

然后您可以像这样调用IMP:

You could then invoke the IMP like so:

theImplementation(self, theSelector, 3, 5);

除非您正在认真进行伏都教活动,否则通常没有理由需要IMPs-您想做些特定的事情吗?

There's usually no reason to need IMPs unless you're doing serious voodoo--is there something specific you want to do?