As with Objective-C’s other language constructs, multiple arguments will probably appear 
strange at first; however, once you become accustomed to them, you will find the syntax easier 
than Java, C++, and other dot-notation languages. Why are we so confident that you will love 
Objective-C’s syntax for multiple arguments? In a word, readability. How many times have 
you seen code like this in a Java program?
objMyClass.startPlay("Adventures of Tom Thumb", 44, 
    new CherryPie( ), "Jack Sprat", 77);
What exactly do the arguments mean? What are you sending to the startPlay method in  objMyClass? Now consider the same method using Objective-C.
 [objMyClass startPlay: @"Adventures of Tom Thumb" audienceMembers:44
     pie: [[CherryPie alloc] init] supportingActor:@"Jack Sprat"
    extrasNeeded:77];
You know exactly what the arguments sent to the method mean when using Objective-C. You 
are starting a play entitled “Adventures of Tom Thumb” that has 44 members in the audience, 
needs a cherry pie, has a supporting actor named Jack Sprat, and requires 77 extras.
The signature of the method called in the previous message has a syntax as follows:
-(void) startPlay: (NSString*) title audienceMembers: (int) value
    pie: (CherryPie*) pievalue supportingActor: (NSString*) actorvalue
    extrasNeeded: (int) extrasvalue;
The first argument is unnamed. The second and any further arguments are distinguished by a 
space followed by an argument name and colon, followed by the type in parentheses, followed 
by a parameter name to hold the value.
Now, here’s the tricky part: When referring to a multiple-argument method, when 
calling the method, you refer to its named arguments. An argument’s named argument is 
the name prior to the argument’s data type. When using the argument within the method’s 
implementation that the argument is a part of, you refer to the actual parameter name, not the 
argument name. So, for instance, in the startPlay method’s implementation, you refer to title, 
value, pievalue, actorvalue, and extrasvalue. When calling the method, you refer to startPlay’s 
named arguments: audienceMembers, pie, supportingActor, and extrasNeeded.
 
No comments:
Post a Comment