views:

153

answers:

5

I have a question about the first paramater in Objective-C

-(NSInteger) totalSeconds:(NSInteger)h minutes:(NSInteger)m seconds:(NSInteger)s;

I've noticed that it seems the first paramater is often 'pulled into' the message name itself and is not named.

[totalSeconds:9 minutes:59 seconds:59]

Is this kind of syntax acceptable:

    -(NSInteger) totalSeconds:hours:(NSInteger)h 
minutes:(NSInteger)m seconds:(NSInteger)s;

I've looked around and haven't seen such an example although I expected it to be common.

+4  A: 

Why not something like:

-(NSInteger) secondsFromHours:(NSInteger)h minutes:(NSInteger)m seconds:(NSInteger)s
Nick Veys
Or something like: `- (NSInteger)totalTimeFromHours:(NSInteger)h minutes:(NSInteger)m seconds:(NSInteger)s`.
mipadi
This is part of my question. Is that the standard way? To adapt the name of the method itself to the first parameter rather than labeling the first parameters as you do the others.
Gazzer
+5  A: 

I would probably name that method

[totalSecondsWithHours:9 minutes:59 seconds:59]

The idea with method naming is that you should be able to read the call and have it seem like an English sentence.

Rudedog
+7  A: 

Your specific syntax will work as a message declaration, but the result will not be what you expect.

-(NSInteger) totalSeconds:hours:(NSInteger)h 
             minutes:(NSInteger)m seconds:(NSInteger)s;

The way the compiler will see this is the following:

-(NSInteger) totalSeconds:(id)hours:
             (NSInteger)h
             minutes:(NSInteger)m
             seconds:(NSInteger)s

hours becomes the parameter name for the id parameter type, not the identifier for the h parameter. In order to call it, the call winds up looking very funky:

[self totalSeconds:nil :12 minutes:50 seconds:42];

Notice that you now have to pass an object as the first parameter (I've chosen nil), and the word hours is no longer in the call.

I would not name the message in this way. As Rudedog has said here, the idea is that you should be able to read the call like an English sentence. Go with a name similar to his or Nick Veys'.

From your comment:

This is part of my question. Is that the standard way? To adapt the name of the method itself to the first parameter rather than labeling the first parameters as you do the others.

Yes, the standard is to name the message such that the first parameter "name" is part of the message itself. Understand that the selector includes all of this information in it. The selector for the message, as named above, is:

totalSeconds::minutes:seconds

As named better, the selector should read something like:

totalSecondsFromHours:minutes:seconds
John Rudy
The default type of a parameter to a method is id. In the example above, "hours" would be assumed to be an object.
NSResponder
@NSResponder: Thanks; updated appropriately.
John Rudy
+4  A: 

The "message name" is not merely its first component but all of the tokens that precede arguments.

Consider a method called totalSeconds. Easy, it's a getter. Makes sense, right? Now consider a method called totalSeconds:. That doesn't make a lot of sense because it doesn't identify what the argument is supposed to be. This is why, instead of foo:, you often see fooWithBar:, and from that pattern you can extend fooWithBar:baz:hax:.

In your first example, the method name was totalSeconds:minutes:seconds:. I think it was a good suggestion to call it secondsFromHours:minutes:seconds:.

Kevin Conner
+2  A: 

This:

[totalSeconds:9 minutes:59 seconds:59]

is not a valid message expression. The syntax of an Objective-C message is:

[receiver message]

What you've written above will attempt to send a -:minutes:seconds: message to totalSeconds

NSResponder
Absolutely true, however I think we all kind of assumed he meant to have a receiver before `totalSeconds`. That may not have been a valid assumption, but appears to have been based on the rest of the activity here. :)
John Rudy