Swift:丢失的参数标签为“xxx”

时间:2021-10-08 09:40:22
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.

为什么?如果我在func中输入了两个以上的var,那么当我调用这个func时,我需要写var名称而不是第一个var,这真的很麻烦,我在iBook Swift教程中看不到任何解释。

5 个解决方案

#1


65  

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.

具体来说,Swift在默认情况下为方法中的第一个参数名提供一个本地参数名,并在默认情况下为第二个和后续的参数名提供本地和外部参数名。此约定与您在编写Objective-C方法时熟悉的典型命名和调用约定相匹配,并且使方法调用具有表达性,而不需要限定参数名。

Notice the autocomplete: Swift:丢失的参数标签为“xxx”

注意到自动完成:

#2


11  

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

这只是Objective-C语言的影响。当调用方法时,方法的第一个参数不需要显式地标记(在Objective-C中,它实际上是由方法的名称标记的)。但是,以下所有参数都需要名称来标识它们。它们也可以取一个(可选的)本地名称在方法本身中使用(请参阅上面注释中的Jiaaro的链接)。

#3


1  

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

这是编译器中的一个怪癖。函数(不是类的成员)和类方法对于命名参数有不同的默认行为。这与objective-C中命名参数的行为一致(但对于没有使用objective-C经验的swift新手来说没有意义)。

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.

有关类方法的信息,请参见Logan的答案。

#4


0  

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

在swift 3.0中,每个输入都有一个参数名的方法必须将该参数名作为函数调用的一部分。如果你像这样定义这个函数

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

#5


0  

Simple:

简单:

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

错误的调用函数语法(在c/c++/java/c#中不一样)

Incorrect:

不正确的:

say("Henry")

Correct:

正确的:

say(name:"Henry")

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

PS:你必须始终!在值之前添加“名称函数参数”。

#1


65  

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.

具体来说,Swift在默认情况下为方法中的第一个参数名提供一个本地参数名,并在默认情况下为第二个和后续的参数名提供本地和外部参数名。此约定与您在编写Objective-C方法时熟悉的典型命名和调用约定相匹配,并且使方法调用具有表达性,而不需要限定参数名。

Notice the autocomplete: Swift:丢失的参数标签为“xxx”

注意到自动完成:

#2


11  

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

这只是Objective-C语言的影响。当调用方法时,方法的第一个参数不需要显式地标记(在Objective-C中,它实际上是由方法的名称标记的)。但是,以下所有参数都需要名称来标识它们。它们也可以取一个(可选的)本地名称在方法本身中使用(请参阅上面注释中的Jiaaro的链接)。

#3


1  

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

这是编译器中的一个怪癖。函数(不是类的成员)和类方法对于命名参数有不同的默认行为。这与objective-C中命名参数的行为一致(但对于没有使用objective-C经验的swift新手来说没有意义)。

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.

有关类方法的信息,请参见Logan的答案。

#4


0  

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

在swift 3.0中,每个输入都有一个参数名的方法必须将该参数名作为函数调用的一部分。如果你像这样定义这个函数

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

#5


0  

Simple:

简单:

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

错误的调用函数语法(在c/c++/java/c#中不一样)

Incorrect:

不正确的:

say("Henry")

Correct:

正确的:

say(name:"Henry")

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

PS:你必须始终!在值之前添加“名称函数参数”。