swift中下划线怎么用
程序员文章站
2023-12-25 09:39:21
...
swift中的下划线,可以用于忽略外部参数名,一般第一个参数的外部名是被自动忽略的。
代码如下
//用下划线前
func myFunc(name: String, age: Int = 20, grade: Int = 2)
{
println("add: name = \(name), age = \(age), grade = \(grade)")
}
myFunc(name:"Bob", age: 19, grade: 2)
//用下划线忽略外部名后
func myFunc(name: String, _ age: Int = 20, _ grade: Int = 2)
{
println("add: name = \(name), age = \(age), grade = \(grade)")
}
myFunc("Bob", 19, 2)