What is use of performSelector in iOS
The two are pretty identical when used as you have demonstrated, but the latter has the advantage that you can dynamically determine which selector to call at runtime.
SEL selector = [self gimmeASelectorToCall];
[self performSelector: selector];
[Source]
Apple doc is your friend.
NSObject Protocol Reference
It
Sends a specified message to the receiver and returns the result of the message.
In particular:
The
performSelector:
method is equivalent to sending an aSelector message directly to the receiver. For example, all three of the following messages do the same thing:id myClone = [anObject copy]; id myClone = [anObject performSelector:@selector(copy)]; id myClone = [anObject performSelector:sel_getUid("copy")];
However, the performSelector: method allows you to send messages that aren’t determined until runtime. A variable selector can be passed as the argument:
SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation(); [anObject performSelector:myMethod];
The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.
Hope that helps.
A selector
object lets you call a method that you do not know at compile time. You need to know only the name of a method as a string in order to call it.
When the name of the method that you are calling is known at compile time, using selectors is counterproductive: the code becomes less readable for no apparent advantage. When you are writing a library that needs to call methods in other code that is compiled separately from the library, selectors provide a way to decouple the two pieces of code.
For example, if you are writing a timer class that can call you back when a time interval is over, your timer does not know the name of the function that it needs to call, so it cannot write something like this:
// We do not know if the function is called intervalHasExpired or something else
[target intervalHasExpired];
But if you give your timer a selector, the timer would be able to call you back.
[myTimer scheduleWithTarget:self andSelector:@selector(myCompletion)];