Graphics Programs Reference
In-Depth Information
selector, and each label ends with a colon. The selector is all of the labels taken together
( Figure 2.2 ) .
Figure 2.2 Parts of a message send
This pairing of labels and arguments is an important feature of Objective-C. In other lan-
guages, this method would look like this:
partyInstance.addAttendeeWithDish(somePerson, deviledEggs);
In these languages, it isn't completely obvious what each of the arguments sent to this
function are. In Objective-C, however, each argument is paired with the appropriate label.
[partyInstance addAttendee:somePerson withDish:deviledEggs];
It takes some getting used to, but eventually, Objective-C programmers appreciate the
clarity of arguments being interposed into the selector. The trick is to remember that for
every pair of square brackets, there is only one message being sent. Even though addAt-
tendee:withDish: has two labels, it is still only one message, and sending that mes-
sage results in only one method being executed.
In Objective-C, the name of a method is what makes it unique. Therefore, a class cannot
have two methods with the same name. However, two methods can have the same indi-
vidual label, as long as the name of each method differs when taken as a whole. For ex-
ample, our Party class has two methods, addAttendee: and addAt-
tendee:withDish: . These are two distinct methods and do not share any code.
Also, notice the distinction being made between a message and a method : a method is a
chunk of code that can be executed, and a message is the act of asking a class or object to
execute a method. The name of a message always matches the name of the method to be
executed.
 
Search WWH ::




Custom Search