Re: ObjC Question - labeled arguments
Re: ObjC Question - labeled arguments
- Subject: Re: ObjC Question - labeled arguments
- From: Peter Zegelin <email@hidden>
- Date: Thu, 22 May 2008 15:25:33 +1000
On 22/05/2008, at 3:09 PM, Andrew Farmer wrote:
On 21 May 08, at 21:48, Peter Zegelin wrote:
After all the talk about the best way to learn Cocoa I thought I'd
better brush up on my obj-c. I've been able to get an amazing
distance without really understanding a lot as I am mainly using
Cocoa as a gui around some C++ code and have been able to get a
long way just by wiring things up in IB and reusing some example
code. However when messaging an object with multiple parameters I
have never been able to use labeled arguments - only unlabeled.
However on a hunch I removed the first label and it worked.
Brush up on your syntax a bit. You seem to misunderstand how ObjC
method calls work.
Well thats what I am trying to do!
- (void)reset:(RulerStyle*)newStyle:(int)newSide:(int)textLoc:
(double)newScale;
That syntax is pretty obscure. Try reading it as
- (void)reset:(RulerStyle*)style
newStyle:(int)newStyle
newSide:(int)newSide
textLoc:(double)newScale;
and see if that makes any more sense. (Some of the types seem a
little odd for the argument names, but that's what the method
signature reads as.)
If this still doesn't make sense, check the header file for the code
you're interfacing with.
I probably should have explained that I am calling my own ruler
implementation so that object and method is my own. In C++ it is:
void reset(RulerStyle* newStyle, int newSide, int textLoc, double
newScale);
So my question is - is there a way to include the first label? I
note that the examples in the ObjectiveC manual seem to leave the
first one out also:
[myRect setWidth:10.0 :15.0]; ---> [myRect setWidth:10.0 height:
15.0];
They shouldn't - that's invalid. Every "label" is a component of the
method name - [myRect setWidth:10.0 height:15.0] is calling a
completely separate method from [myRect setWidth:10.0 somethingElse:
15.0].
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]; "
regards,
Peter
_______________________________________________
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:
This email sent to email@hidden