快速错误:在调用中缺少参数标签“name:”

时间:2022-02-07 02:24:57

I'm learning about default arguments and I ran aground of something weird:

我正在学习默认参数,我遇到了一些奇怪的事情:

import UIKit

func greet(name: String = "world") {
    println("hello \(name)")
}

greet("jiaaro")

this throws an error:

这将抛出一个错误:

Playground execution failed: error: <REPL>:9:7: error: missing argument label 'name:' in call
greet("jiaaro")
      ^
      name:

I understand that it wants greet(name: "jiaaro") but I don't understand why that should be necessary.

我知道它想要打招呼(名字:“jiaaro”),但我不明白为什么这是必须的。

3 个解决方案

#1


11  

Swift functions can specify local and external argument names:

Swift函数可以指定本地和外部参数名:

func greet(who name: String = "world") {
    println("hello \(name)")
}

// prints "hello world"
greet()

// prints "hello jiaaro"
greet(who:"jiaaro")

// error
greet("jiaaro")

// error
greet(name: "jiaaro")

To opt out of this behavior you can use an underscore for the external name. Note that the first parameter implicitly uses the "no external name" behavior:

要退出此行为,可以为外部名称使用下划线。注意,第一个参数隐式地使用“无外部名”行为:

func greet(name: String = "world", _ hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hello world"
greet()

// prints "hello jiaaro"
greet("jiaaro")

// prints "hi jiaaro"
greet("jiaaro", "hi")

// error
greet(name: "jiaaro")

The following is now disallowed in Swift 2.0, see below for equivalent code.

下面的代码在Swift 2.0中是不允许的,参见下面的代码。

You can use the # prefix to use the same local and external name for the first parameter:

您可以使用#前缀为第一个参数使用相同的本地和外部名称:

func greet(#name: String = "world", hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")

Swift 2.0 code:

斯威夫特代码:2.0

func greet(name name: String = "world", hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")

#2


3  

Swift requires argument labels by default, because it supports classes with multiple initializers. The benefit of argument labels comes from the ability of Swift to infer which initializer to use; not only by argument type, but argument name as well.

Swift在默认情况下需要参数标签,因为它支持具有多个初始化器的类。参数标签的好处来自Swift推断使用哪个初始化器的能力;不仅是参数类型,还有参数名。

struct Celsius {
    var temperatureInCelsius: Double = 0.0

    init(fromFahrenheit fahrenheit: Double) {
        temperatureInCelsius = (fahrenheit - 32.0) / 1.8
    }

    init(fromKelvin kelvin: Double) {
        temperatureInCelsius = kelvin - 273.15
    }
}

let boilingPointOfWater = Celsius(fromFahrenheit: 212.0)
// boilingPointOfWater.temperatureInCelsius is 100.0

let freezingPointOfWater = Celsius(fromKelvin: 273.15)
// freezingPointOfWater.temperatureInCelsius is 0.0

See this page for more details: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_272

更多细节请参见此页面:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/initializ.html #/ apple_ref/doc/uid/ tp400097 - ch18 - xid_272

#3


1  

I just wanted to add that now your code

我只是想添加你的代码

func greet(name: String = "world") {
    print("hello \(name)")
}

greet("jiaaro")

works fine in xcode, i just changed "println" with "print"

在xcode中工作得很好,我把println改成了print

#1


11  

Swift functions can specify local and external argument names:

Swift函数可以指定本地和外部参数名:

func greet(who name: String = "world") {
    println("hello \(name)")
}

// prints "hello world"
greet()

// prints "hello jiaaro"
greet(who:"jiaaro")

// error
greet("jiaaro")

// error
greet(name: "jiaaro")

To opt out of this behavior you can use an underscore for the external name. Note that the first parameter implicitly uses the "no external name" behavior:

要退出此行为,可以为外部名称使用下划线。注意,第一个参数隐式地使用“无外部名”行为:

func greet(name: String = "world", _ hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hello world"
greet()

// prints "hello jiaaro"
greet("jiaaro")

// prints "hi jiaaro"
greet("jiaaro", "hi")

// error
greet(name: "jiaaro")

The following is now disallowed in Swift 2.0, see below for equivalent code.

下面的代码在Swift 2.0中是不允许的,参见下面的代码。

You can use the # prefix to use the same local and external name for the first parameter:

您可以使用#前缀为第一个参数使用相同的本地和外部名称:

func greet(#name: String = "world", hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")

Swift 2.0 code:

斯威夫特代码:2.0

func greet(name name: String = "world", hello: String = "hello") {
    println("\(hello) \(name)")
}

// prints "hi jiaaro"
greet(name: "jiaaro", hello: "hi")

#2


3  

Swift requires argument labels by default, because it supports classes with multiple initializers. The benefit of argument labels comes from the ability of Swift to infer which initializer to use; not only by argument type, but argument name as well.

Swift在默认情况下需要参数标签,因为它支持具有多个初始化器的类。参数标签的好处来自Swift推断使用哪个初始化器的能力;不仅是参数类型,还有参数名。

struct Celsius {
    var temperatureInCelsius: Double = 0.0

    init(fromFahrenheit fahrenheit: Double) {
        temperatureInCelsius = (fahrenheit - 32.0) / 1.8
    }

    init(fromKelvin kelvin: Double) {
        temperatureInCelsius = kelvin - 273.15
    }
}

let boilingPointOfWater = Celsius(fromFahrenheit: 212.0)
// boilingPointOfWater.temperatureInCelsius is 100.0

let freezingPointOfWater = Celsius(fromKelvin: 273.15)
// freezingPointOfWater.temperatureInCelsius is 0.0

See this page for more details: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_272

更多细节请参见此页面:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/initializ.html #/ apple_ref/doc/uid/ tp400097 - ch18 - xid_272

#3


1  

I just wanted to add that now your code

我只是想添加你的代码

func greet(name: String = "world") {
    print("hello \(name)")
}

greet("jiaaro")

works fine in xcode, i just changed "println" with "print"

在xcode中工作得很好,我把println改成了print