Graphics Programs Reference
In-Depth Information
Combining two messages in a single line of code is called a
nested message send
. The in-
nermost brackets are evaluated first, so the message
alloc
is sent to the class
Party
first. This returns a new, uninitialized instance of
Party
that is then sent the message
init
.
Sending messages
What do you do with an instance that has been initialized? You send it more messages.
Let's take a closer look at message anatomy. First of all, a message is always contained in
square brackets. Within a pair of square brackets, a message has three parts:
receiver
a pointer to the object being asked to execute a method
selector
the name of the method to be executed
arguments
the values to be supplied as the parameters to the method
A party might have a list of attendees that you can add to by sending the party the mes-
sage
addAttendee:
.
[partyInstance addAttendee:somePerson];
Sending the
addAttendee:
message to
partyInstance
(the receiver) triggers the
addAttendee:
method (named by the selector) and passes in
somePerson
(an argu-
ment).
The
addAttendee:
message has only one argument, but Objective-C methods can take
a number of arguments or none at all. The message
init
, for instance, has no arguments.
An attendee to a party might RSVP with an edible item. Thus, a party may have another
method named
addAttendee:withDish:
. This message takes two arguments: the at-
tendee and the dish the attendee plans to bring. Each argument is paired with a label in the