Objective-C和SEL/IMP的使用 [英] Objective-C and use of SEL/IMP

查看:91
本文介绍了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?

这篇关于Objective-C和SEL/IMP的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆