I need to do a thing like this:
id myFunction = aMethodDeclaredInMyClass;
[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.
SEL myAction = nil; .. some stuff .. myAction = @selector(actionWalk); .. some stuff .. [self performSelector:myAction]; thank you @Jarret HardieMy guess is you want;
[self performSelector:@selector(aMethodDeclaredInMyClass)]
Read the docs on dynamic dispatch;
Also see objc_msgSend(). You'd set up a SEL selector variable.
objc_msgSend directly. It's like swatting flies with a broadsword. There are cases where you want to use it, but they are very special.objc_msgSend() more or less the same as -performSelector:withObject:, but with less overhead? (Genuinely curious.)objc_msgSend is more flexible and easier to misuse without realizing it, so using it directly is kind of a bad habit to get into. My phrasing might have been a little sharp, but I don't want to see innocent Googlezens shooting themselves in the foot unnecessarily.objc_msgSend_stret would be more appropriate? If you start considering these corner cases, you might have defeated the purpose of using Obj-C in the first place. :)