tags:

views:

3648

answers:

5

I'm new to Cocoa/Cocoa Touch, and working through a development book. I've come across situations where the @selector() operator is used. I'm a bit lost on how and when the @selector() operator should be used. Can someone provide a short and sweet explanation and example of why it's used and what benefit it gives the developer?

By the way, here is sample code taken from Apple's iPhone development site that uses @selector()

if ([elementName isEqualToString:@"entry"])
{

    parsedEarthquakesCounter++;

    // An entry in the RSS feed represents an earthquake, so create an instance of it.
    self.currentEarthquakeObject = [[Earthquake alloc] init];
    // Add the new Earthquake object to the application's array of earthquakes.
    [(id)[[UIApplication sharedApplication] delegate]
            performSelectorOnMainThread:@selector(addToEarthquakeList:)
            withObject:self.currentEarthquakeObject waitUntilDone:YES];
    return;
}
+8  A: 

The selector operator provides a way to refer to a method provided by an object, somewhat similar to a function pointer in C. It is useful because it allows you to decouple the process of calling methods on an object. For example one piece of code could provide a method, and another piece of code could apply that method to a given set of objects.

Examples:

Test to see if an object implements a certain method:

[object respondsToSelector:@selector(methodName)]

Store a method to later call on an object;

SEL method = @selector(methodName);
[object performSelector:method];

Call a method on a different thread (useful for GUI work).

[object performSelectorOnMainThread:@selector(methodName)]
Andrew Grant
Thanks for the explanation - can you give an example of why you would use it instead of calling a method in the conventional way, assuming you know that the object supports that method?
lupefiasco
@lupefiasco: [object performSelectorInBackground:@selector(methodName)] will result in an asynchronous call to the method.
codelogic
Ah got it. Thanks!
lupefiasco
Another use case is if you have the selector's name as a string, then you can do, at runtime, something like [object performSelector:NSSelectorFromString(something) ... ]
codelogic
A: 

@selector() is used each time you need to pass the name of a method as an argument to another method, a function or as a variable value. Passing directly the name doesn't work in objective-C.

mouviciel
A: 

Two references to look at:

http://en.wikipedia.org/wiki/Multiple_dispatch

http://thaesofereode.info/clocFAQ/#selimps (and the next few questions)

Graham Lee
+1  A: 

In addition to what's been said, you can also wrap up the @selector in an NSInvocation for later use. You can set the arguments to the NSInvocation a long time after it's created, and activate it when you need the message to be fired. This gives you a lot of power.

For an introduction to the concept, Scott Stevenson has a great post entitled "Dynamic Objective-C with NSInvocation".

Brad Larson
+1  A: 

One practical example is validateMenuItem method where menu items are identified with their target actions.

Simplified example:

- (BOOL)validateMenuItem:(NSMenuItem *)item {
    if ([item action] == @selector(selectFiles:) && otherCondition) {
        return YES;
    } else {
        return NO;
    }
}
Petteri Hietavirta