What is use of performSelector in iOS
Asked Answered
S

5

7

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)];

Sweat answered 18/7, 2012 at 10:39 Comment(0)
K
10

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]

Knickknack answered 18/7, 2012 at 10:42 Comment(0)
S
8

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.

Sabinasabine answered 18/7, 2012 at 10:50 Comment(1)
@stackBlue You're welcome. I marked bold the main parts from Apple doc. Upvote if you want :-) Cheers.Sabinasabine
F
5

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)];
Forseti answered 18/7, 2012 at 10:43 Comment(0)
N
2

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.

Noonberg answered 18/7, 2012 at 10:48 Comment(0)
F
1

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.

Froh answered 18/7, 2012 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.