Swift functions can specify the names of local and external arguments:
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 reject this behavior, you can use the 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")
Swift 2.0 now disables the following: see equivalent code below.
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:
func greet(name name: String = "world", hello: String = "hello") { println("\(hello) \(name)") } // prints "hi jiaaro" greet(name: "jiaaro", hello: "hi")
Jiaaro Jun 04 '14 at 23:29 2014-06-04 23:29
source share