Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjC Question - labeled arguments



On May 21, 2008, at 10:25 PM, Peter Zegelin wrote:
Well that example is straight out of the manual:

"Methods can also take arguments. The imaginary message below tells myRect to set its location within
the window to coordinates (30.0, 50.0):
[myRect setOrigin:30.0 :50.0];
Here the method name, setOrigin::, has two colons, one for each of its arguments. The arguments
are inserted after the colons. This method name uses unlabeled arguments. Unlabeled arguments
make it difficult to determine the kind and purpose of a method’s arguments. Instead, method names
should include labels describing each of their arguments. Argument labels precede each colon in the
method name. The setWidth:height: method, for example, makes the purpose of its two arguments
clear:
[myRect setWidth:10.0 height:15.0]; "

Objective-C does not have named arguments. Nor does it have keyword arguments.


Those are two different methods; two different selectors, to be precise.

The first example invokes the method named -- whereby "method name" is synonymous with "selector" -- "setOrigin::".

The second example invokes the selector "setWidth:height:".

That is, those two lines of code would work if and only if "myRect" implemented BOTH methods that could likely be declared as:

- (void) setOrigin: (float) x :(float) y;
- (void) setWidth: (float) x height: (float) y;

The paragraph implies that the methods are different, but doesn't state it explicitly. If I were writing this, I might say:

....

Methods can also take arguments. The imaginary message below tells the myRect object to set its origin to the coordinates (30.0, 50.):

[myRect setOrigin:30.0 :50.0];

The method is named "setOrigin::". It has two colons as it takes two arguments. This particular method does not interleave the method name with the arguments and, thus, the second argument is effectively unlabeled and it is difficult to determine the kind or purpose of the method's arguments.

Instead, method names should interleave the name with the arguments such that the method's name naturally describes the arguments expected by the method. For example, the myRect class could instead implement a "setOriginX:y:" method that makes the purpose of its two arguments clear:

[myRect setOriginX: 30.0 y: 50.0];
....


b.bum

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >ObjC Question - labeled arguments (From: Peter Zegelin <email@hidden>)
 >Re: ObjC Question - labeled arguments (From: Andrew Farmer <email@hidden>)
 >Re: ObjC Question - labeled arguments (From: Peter Zegelin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.