Swift: missing argument label "xxx" in call

func say(name:String, msg:String) { println("\(name) say \(msg)") } say("Henry","Hi,Swift") <---- error because missing argument label 'msg' in call 

I need to use

  say("Henry",msg:"Hi,Swift") 

Why? If I put more than two variables in func, so I need to write the name var instead of the first var when I call this func
This is really a problem, and I see no explanation in the iBook Swift tutorial.

+64
ios swift
Jun 05 '14 at 2:50
source share
4 answers

One possible reason is that it is actually a method. The methods are very vile, they look the same as regular functions, but they do not work the same way, let's look at this:

 func funFunction(someArg: Int, someOtherArg: Int) { println("funFunction: \(someArg) : \(someOtherArg)") } // No external parameter funFunction(1, 4) func externalParamFunction(externalOne internalOne: Int, externalTwo internalTwo: Int) { println("externalParamFunction: \(internalOne) : \(internalTwo)") } // Requires external parameters externalParamFunction(externalOne: 1, externalTwo: 4) func externalInternalShared(#paramOne: Int, #paramTwo: Int) { println("externalInternalShared: \(paramOne) : \(paramTwo)") } // The '#' basically says, you want your internal and external names to be the same // Note that there been an update in Swift 2 and the above function would have to be written as: func externalInternalShared(paramOne paramOne: Int, #paramTwo: Int) { print("externalInternalShared: \(paramOne) : \(paramTwo)") } externalInternalShared(paramOne: 1, paramTwo: 4) 

Now here's the fun part declaring a function inside a class, and it's not a function anymore ... it's a method

 class SomeClass { func someClassFunctionWithParamOne(paramOne: Int, paramTwo: Int) { println("someClassFunction: \(paramOne) : \(paramTwo)") } } var someInstance = SomeClass() someInstance.someClassFunctionWithParamOne(1, paramTwo: 4) 

This is part of the behavior design for methods.

Apple Docs:

In particular, Swift gives the first parameter name in the method the name of the local default parameter and gives the second and next parameter names by default, both local and external parameter names. This convention corresponds to a typical naming convention that you will be familiar with when writing Objective-C methods, as well as expressing expressive methods without having to define parameter names.

Pay attention to autocomplete: enter image description here

+65
Jun 05 '14 at 4:37
source share

This is just the influence of Objective-C. When calling a method, the first parameter of the method should not be explicitly marked (as in Objective-C it is effectively "marked" by the name of the method). However, all of the following parameters require a name to identify them. They can also use a (optional) local name for use inside the method itself (see the Jiaaro Link in the comments above).

+11
Jun 05 '14 at 3:22
source share

This is a fad in the compiler. Functions (which are not members of the class) and class methods have different default values ​​for named parameters. This is consistent with the behavior of these parameters in objective-C (but it does not make sense for someone new to quickly, without experience with objective-C).

Here, a link to the language should point to named parameters for functions (in particular, parameters where the external parameter name is not specified and the parameter has no default value)

However, these parameter names are used only in the body of the function itself, and cannot be used when calling the function. These types of parameter names are called local parameter names, because they are only available for use in the body of functions.

For information on class methods, see Logan's answer.

+1
Jun 05 '14 at 3:39 on
source share

Swift 3.0 Update:

In swift 3.0, methods with one parameter name for each input are needed to have this parameter name as part of a function call. Therefore, if you define a function like this

 func say(name:String, msg:String) { print("\(name) say \(msg)") } 

Your function call should be like this

 self.say(name: "Henry",msg: "Hi,Swift") 

If you want to have English as readable function labels, but don’t want to change the name of the input parameter, you can add a label before the parameter names, for example,

 func say(somethingBy name:String, whoIsActuallySaying msg:String) { print("\(name) say \(msg)") } 

Then call it as follows

 self.say(somethingBy: "Henry",whoIsActuallySaying: "Hi,Swift") 
0
Jul 14 '17 at 20:01
source share



All Articles