开发者

What is the standard way to write a method declaration in Objective C?

开发者 https://www.devze.com 2022-12-15 12:02 出处:网络
I have a question about the first paramater in Objective-C -(NSInteger) totalSeconds:(NSInteger)h minutes:(NSInteger)m seconds:(NSInteger)s;

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.

-(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


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.


Why not something like:

-(NSInteger) secondsFromHours:(NSInteger)h minutes:(NSInteger)m seconds:(NSInteger)s


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:.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消