Objective-c函数指针 [英] Objective-c function pointer

查看:108
本文介绍了Objective-c函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做这样的事情:

I need to do a thing like this:

id myFunction = aMethodDeclaredInMyClass;

[self myFunction]

获得任何帮助!

推荐答案

如果您事先知道该方法:

If you know the method in advance:

[self performSelector:@selector(myMethod) withObject:nil];

如果您事先不知道方法名称:

If you don't know the method name in advance:

SEL selector = NSSelectorFromString(someSelectorStringYoureGiven);
[self performSelector:selector withObject:nil];

这两个例子都假设你的函数不接受任何参数,也不需要在不同的线程上执行,也不需要延迟执行。这些条件的所有组合都有很多变体(对于更复杂的情况,还有NSInvocation)。在xcode的文档中搜索 performSelector 以查看所有变体。

Both these examples assume your function accepts no arguments, nor requires execution on a different thread, nor requires delayed execution. There are many variants for all combinations of those conditions (and NSInvocation for even more complex cases). Search performSelector in xcode's documentation to see all the variants.

这篇关于Objective-c函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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