Swift : missing argument label 'xxx' in call

IosSwift

Ios Problem Overview


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 var in func so that I need to write var name instead of first var when I call this func
It's really trouble, and I don't see any explain in iBook Swift tutorial.

Ios Solutions


Solution 1 - Ios

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:

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's 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, declare a function inside of a class and it's no longer a function ... 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 design of behavior for methods

Apple Docs:

>Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.

Notice the autocomplete: enter image description here

Solution 2 - Ios

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

Solution 3 - Ios

Simple:

Wrong call function syntax's( its not same in c/c++/java/c#)

Incorrect:

say("Henry")

Correct:

say(name:"Henry")

PS: You must always! add "name function parameter" before value.

Solution 4 - Ios

Swift 3.0 update:

In swift 3.0, methods with one param name per inputs are required to have that param name as part of the function call. So if you define the function like this

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

Your function call will have to be like this

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

If you want to have English like readable function labels but do not want to change input param name, you can add the label in front of the parameter names, like this

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

Then calling it like this

self.say(somethingBy: "Henry",whoIsActuallySaying: "Hi,Swift")

Solution 5 - Ios

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)

> However, these parameter names are only used within the body of the > function itself, and cannot be used when calling the function. These > kinds of parameter names are known as local parameter names, because > they are only available for use within the function’s body.

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

Solution 6 - Ios

Please find the small code for understanding in swift 3+.

func sumInt(a:Int,b:Int){
    print(a+b) // Displays 3 here
}

sumInt(a: 1, b: 2) // not like in other languages

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhenry4343View Question on Stackoverflow
Solution 1 - IosLoganView Answer on Stackoverflow
Solution 2 - IosEphemeraView Answer on Stackoverflow
Solution 3 - IosFortranView Answer on Stackoverflow
Solution 4 - IosFangmingView Answer on Stackoverflow
Solution 5 - Iosuser3386109View Answer on Stackoverflow
Solution 6 - IosLokesh GView Answer on Stackoverflow