The presence of a colon indicates that a method takes an argument. The number of colons is the number of arguments. Whitespace cannot be part of (i.e. inside) the name part of a method signature. Whitespace can precede or follow a colon, the return type, argument type and the argument name without changing the method signature.
Code:
-(void)methodNameWithArg1:(int)arg1 andArg2:(int)arg2;
is the same as
Code:
- (void) methodNameWithArg1 : (int) arg1 andArg2 : (int) arg2;
or for that matter
Code:
- (void) methodNameWithArg1 : (int) arg1 andArg2 : (int) arg2;
the following with whitespace in the name (after With) is invalid and the compiler with generate an error, expecting the method definition to end after the word "With"
Code:
-(void)methodNameWith Arg1:(int)arg1 andArg2:(int)arg2
Dealing with the book's example on p. 98, Objective-C style usually uses method signatures to help document the method. The example chosen is a bit odd since it defines a rectangle with 2 points (opposite diagonals I guess) but hopefully this will clarify a few things.
The C function in the example:
Code:
void defineRect (int x1, int y1, int x2, int y2);
with the call:
Code:
defineRect(23,30,60,120);
could be written as the following Objective-C method:
Code:
-(void)defineRect:(int)x1:(int)y1:(int)x2:(int)y2;
with a call (assuming an instance "newRect" of the class the method is defined in):
Code:
[newRect defineRect:23 :30 :60 :120];
Each colon
must be followed by an argument type in parentheses and an argument name. Common Objective-C style includes a
description of the argument's purpose
preceding the colon, but this is
not required. It is common practice and helps make method names almost "self-documenting". Without the descriptors the purpose of the various arguments is ambiguous. So, the book has defined the method
Code:
-(void)defineRectWithx1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2;
which provides some clarification as to the purpose of each argument.
Prepositions such as
For or
With in method signatures frequently separate a more general method name from one that is more specific, and what follows these words is usually an argument description.
So, for example a method for a default rectangle could be
A method for a rectangle of a specific origin and size might be
Code:
-(void)defineRectWithOrigin:(NSPoint)origin andSize:(NSSize)size;
which could also be written:
Code:
-(void)defineRect:(NSPoint)origin :(NSSize)size;
The
WithOrigin and
andSize are not needed.
Calling both versions of this message
Code:
[newRect defineRectWithOrigin:origin andSize:size];
[newRect defineRect:origin :size];
With reasonable variable names they are both quite readable, but the more verbose one is clearer if the variables are more perfunctory. e.g.
Code:
[newRect defineRectWithOrigin:o andSize:s];
[newRect defineRect:o :s];
or without structs
Code:
-(void)defineRectWithOriginX:(float)x originY:(float)y height:(float)height andWidth:(float)width;
[newRect defineRectWithOriginX:2.0 originY:4.0 height:24.0 andWidth:48.0];
versus
Code:
-(void)defineRect:(float)x :(float)y :(float)h :(float)w;
[newRect defineRect:2.0:4.0:24.0:48.0];
The highly verbose naming is not to everyone's taste, but with autofill minimizing the amount of typing, it can add a lot of clarity.
Bob