Swift : missing argument label 'xxx' in call
http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call
I need to use
Why ? If I put more than two var in func so that I need to write var name instead of first var when I call this func |
|||||||||||||||||
|
One possible reason is that it is actually a method. Methods are very sneaky, they look just like regular functions, but they don't act the same way, let's look at this:
Now here's the fun part, declare a function inside of a class and it's no longer a function ... it's a method
This is part of the design of behavior for methods Apple Docs:
Notice the autocomplete: |
|||||||||
|
This is simply an influence of the Objective-C language. When calling a method, the first parameter of a method does not need to be explicitly labelled (as in Objective-C it is effectively 'labelled' by the name of the method). However all following parameters DO need a name to identify them. They may also take an (optional) local name for use inside the method itself (see Jiaaro's link in the comments above). |
|||||
|
This is a quirk in the compiler. Functions (which are not members of a class) and class methods have different default behavior with regards to named parameters. This is consistent with the behavior of named parameters in objective-C (but makes no sense for someone new to swift with no experience with objective-C). Here's what the language reference has to say about named parameters for functions (specifically parameters where an external name for the parameter is not given, and the parameter does not have a default value)
For information about class methods, see Logan's answer. |
|||||||||
|