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.
Swift functions can specify local and external argument names:
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:
You can use the
#
prefix to use the same local and external name for the first parameter: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.
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
I just wanted to add that now your code
works fine in xcode, i just changed "println" with "print"