27

I have a method:

- (void)pan:(double)lat longitude: (double) lon{...}

When I call it Xcode shows to like this:

[self pan:(double) longitude:(double)]

Isn't it possible to set first parameter somehow, like the second (longitude), that Xcode could show like this:

[self pan: latitude:(double) longitude:(double)]

It is very annoying for me that I can not see the name of the first parameter when calling. Is it my fault or it is impossible?

Tom
  • 3,899
  • 22
  • 78
  • 137
  • 5
    What you call a parameter name is actually part of the method name. This is difficult to understand if you know only languages like Java or C++ but the method name is actually split into several parts in obj-c. Your method name is not `pan`, it's `pan:longitude:` Usually you should try to write method names that read like a sentence, e.g `panToLatitude:andLongitude:` – Sulthan Jan 07 '12 at 19:17

3 Answers3

22

The normal way to do what you're asking is to declare your method like:

-(void)panLatitude:(double)lat longitude:(double)lon;

That is, include the label for the first argument with the method name at the beginning. You'd use it like:

[self panLatitude:x longitude:y];
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 3
    So change method name, because it is impossible to define name for the first param? – Tom Jan 07 '12 at 18:13
  • 3
    @Tom, parameters don't really have names, but yes, you will need to change the method name. – Carl Norum Jan 07 '12 at 18:14
  • 1
    Ok. I come form java and c# so it was very strange for me. And to define as panLatitude is strange...because I don't pan to a latitude, but to a latitude AND a longitude... – Tom Jan 07 '12 at 18:15
  • 4
    `panToLatitude:longitude:` would be appropriate and correct here. (Adding "and" is ok, but usually superfluous. It's more appropriate when the properties are unrelated like `setValue:andReloadURL:`, but in most cases I discourage including `and`). As Carl noted, ObjC doesn't have named parameters. It simply includes parameters within the selector. `panToLatitude:longitude:` is a completely different method from `panToLongitude:latitude:`. – Rob Napier Jan 07 '12 at 19:14
  • @Tom 100 agree. Quite a flaw in the language. – Carson Holzheimer Mar 17 '21 at 06:34
11

You just need to name the method differently.

- (void)panToLatitude: (double)lat longitude: (double)lon;

Objective-C doesn't actually have named parameters. The name of the method is (in your example, not my version above) -pan:longitude: and the language uses infix notation to pass the arguments.

5

Remember that objective-c is about sending messages to objects - the message is the args. So the first part of the message often contains the verb and first piece of data. It should read easily.

Do not think of the method name as "pan". In C# and Java the method name would be Pan and the args would be longitude and latitude. Your method should be something like.

- (void) panToLatitude:longitude:

That string is your function (message) name. Think of it as send a message that is a dictionary and the first item contains the verb. I come from C langs background as well so it's a different way of thinking.

bryanmac
  • 38,941
  • 11
  • 91
  • 99