What is use of performSelector in iOS
Solution 1
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];
Solution 2
Apple doc is your friend.
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.
Solution 3
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)];
Solution 4
PerformSelector basically allows you to decide what message to pass during runtime (late binding), as opposed to languages like plain C. If you know the name to a method in an objective C class, you can use
NSSelectorFromString()
To convert a string into a selector, and have your class call that selector using performSelector. In this way, you can choose different functions to call during runtime. You can even select which function to call using a config file.
Solution 5
performSelector
calls a method that has been declared and implemented in the class. It is used when you need to attach an action in code to a event.
![QueueOverFlow](https://i.stack.imgur.com/f3thD.png?s=256&g=1)
QueueOverFlow
I am a Iphone Application Developer and I Love my work :)
Updated on July 27, 2022Comments
-
QueueOverFlow almost 2 years
What is the role of performSelector?
Comparing:
[self btnClicked];
and
[self performSelector:@selector(btnClicked)]; -(void)btnClicked { NSLog(@"Method Called"); }
both are woking fine for me. What is difference between these two.
[self btnClicked]
and[self performSelector:@selector(btnClicked)]
;