In Objective C you have the concept of sending messages to other objects, and, well this is very similar to method calling in languages like C# and Java.
But what exactly are the subtle differences? How should I think of messaging when thinking about my code?
Note: Just a bit of background here, I'm a C#/Java developer trying to understand some concepts about Objective C.
Best Answer
A message is the name of a selector, and the parameters for that selector.
A selector is a symbol.
A method is a piece of code in a class identified by a selector.
In other words,
[foo bar: baz]
says "send the message called@selector(bar:)
with parameterbaz
to objectfoo
. You could send that message to many different objects.In contrast, the method
bar:
for aFoo
might look likebut for a
FooTwo
might look like(I hope I have the syntax right; it's been a while since I last touched Objective-C.)
When you send the message, the Objective-C kernel dispatches the message to
foo
which decides whether it understands the message. It decides this based on whether it can find a method identified by that selector.Two methods with the same name, and one message.
It's also possible for an object to simply forward a particular message (or set of messages) to another object for processing. In this case, you send a message to this proxy object, which has no methods to match that message, and the proxy forwards the message to its wrapped object.