What the difference between a method, a selector and a message in Objective-C?
-
6Related: [Calling Method or Sending Message?](http://stackoverflow.com/questions/2852381/calling-a-method-or-sending-a-message-in-objective-c) [Method call or performSelector](http://stackoverflow.com/questions/2674827/using-self-method-or-selectormethod) Similar: [Difference between signature and selector](http://stackoverflow.com/questions/3095015/whats-the-difference-between-the-method-signature-and-the-selector-in-objective) – jscs Apr 10 '11 at 01:30
3 Answers
This is a great question.
Selector - a Selector is the name of a method. You're very familiar with these selectors:
alloc
,init
,release
,dictionaryWithObjectsAndKeys:
,setObject:forKey:
, etc. Note that the colon is part of the selector; it's how we identify that this method requires parameters. Also (though it's extremely rare), you can have selectors like this:doFoo:::
. This is a method that takes three parameters, and you'd invoke it like[someObject doFoo:arg1 :arg2 :arg3]
. There's no requirement that there be letters before each part of the selector components. As I said, this is extremely rare, and you will not find it used in the Cocoa frameworks. You can work with selectors directly in Cocoa. They have the typeSEL
:SEL aSelector = @selector(doSomething:)
orSEL aSelector = NSSelectorFromString(@"doSomething:");
Message - a message is a selector and the arguments you are sending with it. If I say
[dictionary setObject:obj forKey:key]
, then the "message" is the selectorsetObject:forKey:
plus the argumentsobj
andkey
. Messages can be encapsulated in anNSInvocation
object for later invocation. Messages are sent to a receiver. (ie, the object that "receives" the message).Method - a method is a combination of a selector and an implementation (and accompanying metadata). The "implementation" is the actual block of code; it's a function pointer (an
IMP
). An actual method can be retrieved internally using aMethod
struct (retrievable from the runtime).
Some other related things that you didn't ask for:
Method Signature - a method signature represents the data types returned by and accepted by a method. They can be represented at runtime via an
NSMethodSignature
and (in some cases) a rawchar*
.Implementation - the actual executable code of a method. Its type at runtime is an
IMP
, and it's really just a function pointer. iOS 4.3 includes a new ability to turn a block into anIMP
. This is really cool.
One of the fun things to realize is that the name of a method (the selector) is distinct from the implementation of the method (the IMP). This means that you can swap them around, if you're feeling daring. You can also add and remove methods at runtime, because all you're doing is editing an entry in a hash table: the key is the selector, and the value is the IMP
of the method. This allows you to do some really crazy and trippy stuff. It's not for the faint of heart. :)

- 242,470
- 58
- 448
- 498
-
This is a great Answer :D 1. so selector is more like the signature/path **to** the method itself? Letting the compiler know, this is how you find it? 2. I take that this is more efficient than passing the method itself. Right? 3. Because every selector and method have a 1:1 relation ship and because of this efficiency it's that why you don't pass the method. 4. Also can you give a dumb example of _You can also add and remove methods at runtime_ I've always heard of it, but **never** ever I've needed to do such, nor seen it I _think_ . – mfaani Apr 10 '18 at 21:54
-
5a. Is it that I can pass a conditional parameter which switches between selectors? 5b. instead of passing a conditional parameter to the selector...why not just create one method with an `if` statement? I guess it's because without the `if`s our methods become more _pure_ method that do what their told. Concluding that 'state handling' and the 'action' revolving around it shouldn't be in the same method. – mfaani Apr 10 '18 at 22:29
A method is the implementation which is run when an object or class is asked to perform some action. It is in the scope of its containing class and is therefore different when referenced through some other class. A selector is an identifier which represents the name of a method. It is not related to any specific class or method, and can be used to describe a method of any class, whether it is a class or instance method.
Simply, a selector is like a key in a dictionary. It can tell you what method someone is talking about, but only if you also have the dictionary itself (the class or object). The method is what you get when you ask for the value from the dictionary using the selector as a key.

- 39,734
- 6
- 101
- 123
This site has a good overview of all the terminology in question: http://www.otierney.net/objective-c.html
Check out the link, but I'll give a quick summary:
A method is essentially like a method of function that you are used to in your favourite programming language.
A message (from the article) "A message can be dynamically forwarded to another object. Calling a message on an object in Objective-C doesn't mean that the object implements that message, just that it knows how to respond to it somehow via directly implementing it or forwarding the message to an object that does know how to."
Selectors can mean two things. It can refer to the name of a method, or "refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL." (from: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html)

- 7,978
- 2
- 18
- 22