ios Swift:调用中缺少参数标签“xxx”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24050844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:29:20  来源:igfitidea点击:

Swift : missing argument label 'xxx' in call

iosswift

提问by henry4343

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 name而不是第一个var,
真的很麻烦,而且我在iBook Swift教程中没有看到任何解释。

采纳答案by Logan

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: enter image description here

注意自动完成: 在此处输入图片说明

回答by Ephemera

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 链接)。

回答by Fangming

Swift 3.0 update:

斯威夫特 3.0 更新:

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

回答by Fortran

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:你必须永远!在值前添加“名称函数参数”。

回答by user3386109

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 cannotbe 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 的回答。

回答by Lokesh G

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

请在 swift 3+ 中找到用于理解的小代码。

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

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