BQuadra
BQuadra

Reputation: 819

Objective-c function pointer

I need to do a thing like this:

id myFunction = aMethodDeclaredInMyClass;

[self myFunction]

any help is appreciated!

Upvotes: 2

Views: 5258

Answers (3)

Alex Reynolds
Alex Reynolds

Reputation: 96967

Also see objc_msgSend(). You'd set up a SEL selector variable.

Upvotes: 0

Jarret Hardie
Jarret Hardie

Reputation: 97962

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.

Upvotes: 5

Related Questions