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 Method naming



On Monday, May 28, 2001, at 02:12 PM, Scott wrote:
> Does this mean that I could write my method calls as:
>
> [myRect setWidth:10.0, 10.5];

Nope!

> Would it be acceptable without any parameter names, just commas? Or,
> do I
> have to use colons?
>
> [myRect setWidth:10.0 :10.5];

Can't do this either... the method's name really is "setWidth:height:",
and not anything less.

> Just trying to get as close to a c++ style calling structure as possible
> since that's the way my brain's wired right now. Fewer keystrokes is
> better.

You'll have to break that brain wiring, and the tendency to think that
fewer keystrokes is always preferable, or you are always going to hate
Objective-C. :-)

> Also, what does the minus sign ("-") mean in the declaration? I've
> seen the
> plus sign used also. What's the difference?

A method name with a minus in the declaration means that it is an
instance method, while a plus means that it is a class method. You call
instance methods on objects:
[myRect setWidth:10.0 height:10.5];

you call class methods on classes:
[Rectangle defaultSize];

Note that neither Java nor C++ has _true_ class methods, but you can
think of them as being similar to C++ static methods most of the time.

> [...] And when I declare a method, do I HAVE to declare it with
> parameter names or can I leave them out?
>
> Ex.
> - (void) setWidth:(float) w height:(float) h

Once again, they aren't parameter names, it's all part of the method
name. If you declare the method as:
- (void) set: (float)w :(float)h;

You can do that, and the method name will be "set::". I occasionally see
code from recent C++ converts that looks like that. Anyone else who uses
your code will hate you because they'll have to write:
[myRect set:10.0 :10.5];

(Am I setting the size? The origin? That this rectangle is to be sold
for somewhere between ten bucks and ten fifty? Sometimes a few more
keystrokes can be a lot of help...)

> P.S. I miss Bjarne Stroustrup!

You won't for long. Once you get going I can almost guarantee that
you'll prefer Objective-C. Everyone else seems to. (The one major
exception are the mathematicians among us. Writing things like
quaternion classes that do operator overloading is more convenient in
C++. That comes at the huge cost of all the other C++ programmers
overloading operators willy-nilly to do who knows what.)

--Greg


References: 
 >Re: ObjC Method naming (From: Scott <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.