Re: ObjC Question - labeled arguments
Re: ObjC Question - labeled arguments
- Subject: Re: ObjC Question - labeled arguments
- From: "Michael Ash" <email@hidden>
- Date: Thu, 22 May 2008 13:38:36 +0800
On Thu, May 22, 2008 at 1:34 PM, Peter Zegelin
<email@hidden> wrote:
>
> On 22/05/2008, at 3:15 PM, mmalc crawford wrote:
>
>>
>> On May 21, 2008, at 9:48 PM, Peter Zegelin wrote:
>>
>>> 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];
>>>
>> No, it doesn't.
>>
>>
>> [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];
>>
>>
>> <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_2_section_3.html>
>>
>> mmalc
>>
>
> Well now I'm a bit confused as I just used the same paragraph to show that
> it does. Too much C++ thinking I guess. I'm thinking the method name is
> setWidth with two arguments, one unlabeled and the other labeled with
> 'height'. Any suggestions where I should brush up on this - apart from the
> manual I'm looking at already?
You're misunderstanding the docs here. It doesn't say that the two are
equivalent. Rather it says that the two are *similar*, and that the
latter is preferred because it's more verbose.
In C++, you might compare these two function names:
DoSomething()
DoSomethingWithString()
The latter gives you more information about what the function does and
what kind of parameter it takes. Even though they share a common
prefix, the compiler considers them to be unrelated. The relationship
is only for the human.
Likewise in Objective-C, the method name is the *entire* thing,
"labels" and all, so these are all different methods:
-doSomething (no parameters)
-doSomething: (one parameter)
-doSomething:withString: (two parameters)
-doSomething:withString:andInteger: (three parameters)
-doSomething:andInteger:withString: (three parameters)
-doSomething:: (two parameters)
More properly, the arguments aren't labeled, but rather they are
interleaved with the method name. This is why the whole thing is
significant, and you can't just change the order around and still
refer to the same method.
Mike
_______________________________________________
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