Objective-c函数指针

BQu*_*dra 2 pointers function objective-c

我需要这样做:

id myFunction = aMethodDeclaredInMyClass;

[self myFunction]
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

Jar*_*die 5

如果您事先知道该方法:

[self performSelector:@selector(myMethod) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

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

SEL selector = NSSelectorFromString(someSelectorStringYoureGiven);
[self performSelector:selector withObject:nil];
Run Code Online (Sandbox Code Playgroud)

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