learner2010
learner2010

Reputation: 4167

When and why to use performSelector - iOS

I have been trying to figure out why and how to use performSelector. I did come across Apple Docs. However, I am not able to fully understand it.

Would anyone be able to help me out in explaining this?

Upvotes: 0

Views: 548

Answers (2)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

AFAIK, using the selector is a way to declare the callback method as what we did on Java, C#, ... Suppose that you develop library A, which do an asynchronous calculate and return result whenever it done. At development time, you don't know what call back method you should call whenever the calculating done. So you can make the library get an selector as input parameter and use performSelector later to invoke callback methods.

Hope this helps.

Upvotes: 0

Alladinian
Alladinian

Reputation: 35636

From Apple's documentation:

the performSelector: method allows you to send messages that aren’t determined until runtime

Longer story:

You can send messages to objects without prior knowledge of whether the object implements this particular method. For example:

NSString *astring = @"test";

[test dance]; // Doesn't compile
[test performSelector:@selector(dance)]; // Doesn't make sense but compiles 

Upvotes: 1

Related Questions