Re: ObjC Method naming
Re: ObjC Method naming
- Subject: Re: ObjC Method naming
- From: "Dennis C. De Mars" <email@hidden>
- Date: Mon, 28 May 2001 12:47:30 -0700
on 5/28/01 11:41 AM, Pedrum Mohageri at email@hidden wrote:
>
I have a silly question about the style convention used in Objective C
>
code. Take this example from page 56 of the ObjC reference.
>
>
[myRect setWidth:10.0 height:15.0];
>
>
Does this mean there is a method called set with two parameters width and
>
height? Wouldn't that be represented like this:
>
>
[myRect set Width:10.0 height:15.0];
Basically, it is supposed to be read as if it were the latter (that is,
"Width" identifies the meaning of the first parameter and "height"
identifies the meaning of the second parameter) but you have to write it as
the former.
>
Or does it mean there a method called setWidth with two parameters, a
>
nameless one(default?) and height.
Technically, the name of the selector is setWidth:height: (colons must be
included). The method is the subroutine that this particular object
identifies with the selector. There has to be a colon for each parameter
passed and you put each parameter after its corresponding colon.
It's important to remember this distinction -- the selector in this case is
setWidth:height:, not setWidth. So, for example, you could define a
different selector such as the following:
[myRect setWidth:10.0 bottom:5.0 top:20.0];
and it would call a different method, this one associated with the selector
setWidth:bottom:top: .
Semantically, you are supposed to think of the "set" part as telling you
what the method will do and the "Width" part as telling you what the first
parameter is, but this is purely a convention not enforced by the compiler.
This way of doing things simulates having named parameters and the ability
to overload functions, although in reality it is not treated much
differently from the compiler's point of view from having selectors as
follows:
[myRect SetWidthHeight: 10.0, 15.0]
[myRect SetWidthBottomTop: 10.0, 5.0, 20.0]
...but having the ability to intersperse parameters after the text that
describes them makes method calls much more readable.
- Dennis D.