If i have a string 'A' to represent name of a function. Is there anyway i can check if a class contain function 'A'? I want to this while the app is running.
something like
if("function_nameA" in classA)
[classA function_nameA]
Thanks
A fast, easy to use, free, and community supported 2D game engine
If i have a string 'A' to represent name of a function. Is there anyway i can check if a class contain function 'A'? I want to this while the app is running.
something like
if("function_nameA" in classA)
[classA function_nameA]
Thanks
you could do this:
if([myObject respondsToSelector@selector(myFunction)]) {
[myObject myFunction];
}
Pretty useful when trying to call a function on a class but not being sure whether it's an enemy or a player...
Hope this helps, Natan Avra.
You can also do [myobject performSelector:@selector(myMethod)]. keep in mind you can also create a selector from a string.
You are correct. It will throw an error if there isn't a method. Using the performSelector prevents you from having to create a big if-else mess after you do the "respondsToSelector". It also let's you import selectors from XML files and things if you use the stringToSelector tool.
You could try something like this:
NSString *methodString = [NSString stringWithString:@"myMethod"];
SEL methodSelector = NSSelectorFromString(methodString);
[myObject performSelector:methodSelector];
This should work I guess.
~ Natan Avra.
You must log in to post.