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.
Your specific syntax will work as a message declaration, but the result will not be what you expect.
The way the compiler will see this is the following:
hoursbecomes the parameter name for theidparameter type, not the identifier for thehparameter. In order to call it, the call winds up looking very funky:Notice that you now have to pass an object as the first parameter (I’ve chosen
nil), and the wordhoursis 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:
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:
As named better, the selector should read something like: