我的另一个问题是关于 optimizing Objective C programs启发如下:当方法有两个(或更多)整数输入时,有人有使用 SEL 和 IMP 的简短示例吗?
最佳答案
这是一个 good tutorial用于获取当前 IMP(带有 IMP 概述)。 IMP 和 SEL 的一个非常基本的例子是:
- (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:
theImplementation(self, theSelector, 3, 5);
通常没有理由需要 IMP,除非你正在做严肃的巫术——你有什么特别想做的吗?
关于Objective-C 和 SEL/IMP 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2650190/