Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I have a question about a part of a book I am learning from.

The line is
Code:
-(void) setWidth: (int) w andHeight: (int) h;

I understand that 'h' is the argument of type '(int), then the ':' allows 'Height', the variable, to except that argument. That is the code that I write in my @implementation section. My question is with the words SET and AND, Are they just in the code to make it more readable or are they needed to execute the code correctly. Could it also be written like this?

Code:
-(void) Width: (int) w Height: (int) h;
 
All of the text except the '-', the types, and the ':'s is "ornamental" in the sense that it's only there for readability and to provide a unique identifier.

Code:
- (void) set:(int)w :(int)h;
would be valid, but horrible
Code:
- (void) :(int)w :(int)h;
might even be valid, I can't remember.
Code:
- (void) width:(int)w height:(int)h;
would also be valid, but wouldn't be idiomatic.

fwiw I would probably declare this method as

Code:
- (void) setWidth:(int)w height:(int)h;

since the 'and' is usually omitted.

or, best of all:

Code:
- (void) setSize:(NSSize)size;

Apple has guidelines for naming things here:

http://developer.apple.com/mac/libr...l/CodingGuidelines/Articles/NamingBasics.html

http://developer.apple.com/mac/libr.../CodingGuidelines/Articles/NamingMethods.html
 
Thanks catfish...

I will look over those links. So is the word SET over looked by the compiler then, or what I have been calling an 'ignore' word.

I could also write it like this then using 2 SET words.

Code:
-(void) setWidth: (int) w setHeight: (int) h;
 
The word "set" is not a separate token in that. The compiler doesn't divide things at capitalization changes. What the compiler sees is roughly (using [] to separate chunks):

[ - ] [ (type) ] [ name ] [ : ] [ (type) ] [ argName ] [ moreName ] [ : ] [ (type) ] [ argName ]

It doesn't make decisions on compiling your code based on partial pieces of those chunks like "set".

There *are* a few things that do look at the actual naming of things. Key-Value Coding will look for a correctly named setter function to do its work, and the clang static analyzer understands how Cocoa idioms apply to memory management.

Finally, understanding the pieces of an Objective-C method name is important when you need to refer to them via selectors. To get a selector for your original method you would do:

Code:
@selector(setWidth:andHeight:);
 
I have a question about a part of a book I am learning from.

The line is
Code:
-(void) setWidth: (int) w andHeight: (int) h;

I understand that 'h' is the argument of type '(int), then the ':' allows 'Height', the variable, to except that argument. That is the code that I write in my @implementation section. My question is with the words SET and AND, Are they just in the code to make it more readable or are they needed to execute the code correctly. Could it also be written like this?

Code:
-(void) Width: (int) w Height: (int) h;

There are a few things that I don't understand, English not being my first language. What do you mean by "to except that argument"? I didn't know "except" was a verb. Do you mean "to exclude the argument" or "to mark it as exceptional"? This doesn't make sense.

And that should answer your question: "set" and "and" are not _just_ in the code to make it more readable. They are there to make it _more readable_, and that is very, very important. In my company they would be essential to pass a code review, which means without them you'd have to rewrite the code to get it accepted, and your collegues would be wondering what is wrong with you to think that code that doesn't clearly express its intention might be acceptable.

Anyway, could you try to repost your question in correct and precise English, because I have a vague idea that there are a few things that you don't understand correctly about Objective-C, but I won't start making wild guesses.
 
There are a few things that I don't understand, English not being my first language. What do you mean by "to except that argument"? I didn't know "except" was a verb. Do you mean "to exclude the argument" or "to mark it as exceptional"? This doesn't make sense.

"except" should be "accept", to make sense.
 
Please correct me on this:

If you use @property with @synthesize, the symbolic language stops being entirely ornamental. If you synthesize the above property "width" (not as read-only), the compiler will generate a method equivalent to
Code:
-(void)setWidth:(int)w
{
width = w;
}
( This, of course, would not in any way affect the method -setWidth:andHeight: )

I have not used properties, so I am not clear on all the subtleties. If you synthesize a property but still write a setter method, I believe your setter method would override the synthesized method as long as the method is declared in the .h file - if you write an undeclared setter method, I would expect a compiler error, but doing so would not make much sense anyway.

And "Grammer" is an actor who played Frasier Crane for many many years.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.