Search code examples
iphoneobjective-cperformselector

When to use selectors in Objective C


Possible Duplicate:
Using -performSelector: vs. just calling the method

I can accomplish the same task in my application by doing:

[self performSelector:@selector(displayOneLife)];

and:

[self displayOneLife];

Is it better to use one or the other depending on the situation? If so, can someone elaborate for me? I would just like to use Objective-C best practices.

Thank you, Joey


Solution

  • The -performSelector approach is usually used when you would like to invoke some selector not known at compilation time. For instance, UIButton uses it to invoke the action you wire it up to when a user hits it (the button knows the name of the method it is hooked up in IB and the class it it hooked up to).

    For all other cases you should go with the latter as you don't want to convert your code into an unreadable puzzle.

    P.S. -performSelector + dynamic selector name construction can be used to work around Apple's static analyzer which seeks the binary for prohibited invocations :)