Objective-c function pointer
I need to do a thing like this:
id myFunction = aMethodDeclaredInMyClass;
开发者_C百科
[self myFunction]
any help is appreciated!
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];
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.
My guess is you want;
[self performSelector:@selector(aMethodDeclaredInMyClass)]
Read the docs on dynamic dispatch;
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html%23//apple_ref/doc/uid/TP30001163-CH23-SW1
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40008048
Also see objc_msgSend()
. You'd set up a SEL
selector variable.
精彩评论