我正在学习默认参数,我抛弃了一些奇怪的东西:
import UIKit func greet(name: String = "world") { println("hello \(name)") } greet("jiaaro")
这会抛出一个错误:
06001
我明白,它想要迎接(名称:“jiaaro”),但我不明白为什么这是必要的.
Swift函数可以指定本地和外部参数名称:
原文链接:https://www.f2er.com/swift/318760.htmlfunc 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")
要退出此行为,您可以使用外部名称的下划线.请注意,第一个参数隐含地使用“no external name”行为:
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.
您可以使用#前缀为第一个参数使用相同的本地和外部名称:
func greet(#name: String = "world",hello: String = "hello") { println("\(hello) \(name)") } // prints "hi jiaaro" greet(name: "jiaaro",hello: "hi")
Swift 2.0 code:
06003