Are selectors in Objective-C just another way to send a message to an object? I really don't understand why or how to use them.
5 Answers
Selectors are usually used when you want to define a callback mechanism. The most common use case for selectors in Cocoa is with controls, such as buttons. A UIButton
is very generic, and as such has no idea what should happen when the button is pressed. Before you can use one, you need to tell it what method should be run when the button is pressed. This is done as follows:
[myButton addTarget:self
action:@selector(myButtonWasPressed)
forControlEvents:UIControlEventTouchUpInside];
- (void)myButtonWasPressed {
// Do something about it
}
Then, when the button is pressed, the button will call the selector on the target we passed it. With this mechanism, you don't need to subclass a button every time you want it to call some of your own code. Instead, UIButton
itself has a generic mechanism for dispatching to any code you choose. (Okay, technically, it's the superclass UIControl
that's providing the dispatch mechanism.)

- 48,806
- 11
- 116
- 129
They aren’t another way to send a message to an object, they’re the only way. For example, in [myView setValue:@"foo"]
, setValue:
is a selector. (Another, less convenient way of writing the same thing is objc_msgSend(myView, @selector(setValue:), @"foo")
.)
As Ian Henry says, you can use SEL
values to choose a selector at runtime instead of compile time. This is a fundamental technique in Cocoa; user interfaces are generally connected to controllers using target/action bindings, where the target is an object and the action is a selector. Normally you set this up in a nib, but you can also do it in code:
[myButton setTarget:myController];
[myButton setAction:@selector(buttonClicked:)]; // Clicking the button will now call [myController buttonClick:myButton].

- 14,532
- 3
- 33
- 47
-
1Great answer, but what is the advantage of doing: [myButton setAction:@selector(buttonClicked)] Instead of sending the message to myController directly with a "normal" message? – lampShade Aug 22 '10 at 22:11
-
3By setting the action, the message is sent when the button is clicked. If you send the message directly, it happens “now” (i.e., when the UI is set up). Having all your buttons clicked immediately when you load a window isn’t generally useful. – Jens Ayton Aug 22 '10 at 23:06
-
1Let me make sure I understand what you're saying. lets say that I have a button, myButton that is in the UI. If I set up a "normal" target action in interface builder. Lets' say that I have a method called addItem. When the UI is loaded the addItem method is automatically called - even if the user has not pushed the button? – lampShade Aug 22 '10 at 23:17
-
1No--think of a selector as the *name* of a method. When you set a button's action, you're telling that button the *name* of the message it should send when the user clicks it. So the button can send that message as many times as it needs to, when it needs to, and you-the-programmer don't need to be involved. – Jonathan Grynspan Aug 22 '10 at 23:22
-
3lampShade: No, that's what happens if you send the `addItem:` message to yourself directly *instead* of setting it as the action of the button. The button becomes not involved. The button is only involved when you set yourself as its target (in IB or in code; it doesn't matter which) and `addItem:` as its action, and the message is only sent when the user clicks on the button, which causes the button to send its action message to its target. (Also, selectors for actions in Cocoa must take one argument, hence the : I've appended to `addItem:` in this comment.) – Peter Hosey Aug 23 '10 at 03:58
You can store selectors as variables, and invoke them later or in a different context. For example you can tell an object to perform a selector at a particular time, or on a different thread. You can also choose which selector to perform based on data, which is how interface builder and core data do their thing.
At the most basic, yes, but you can change the message at runtime. For example:
SEL a = [selectorFactory getSelector];
[someOtherObject performSelector:a];
And then in selectorFactory.getSelector
:
if(foo == 1)
return @selector(thisSelector);
else
return @selector(thatSelector);
Coming from C#
or another similar language, you can use this to (loosely) simulate events much more easily than using NSNotification
s. For example, you could make a button class with two ivars, target
and selector
, and have the button perform the selector on the target when it's clicked (for example).
There's a lot more to selectors than that, though. Read more about them here:

- 22,255
- 4
- 50
- 61
--From Apple Developer Library --
A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique. What makes a selector useful is that (in conjunction with the runtime) it acts like a dynamic function pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it’s used with. Suppose you had a selector for the method run, and classes Dog, Athlete, and ComputerSimulation (each of which implemented a method run). The selector could be used with an instance of each of the classes to invoke its run method—even though the implementation might be different for each.

- 6,123
- 34
- 33