代码实例
没有怎么接触过Swift,直接上手看的代码,遇到_
符号,特此记录。
let _ = 100
var _ = 200
for _ in 0...3 {
print("do something...")
}
func fun1( _ x: Int, _ y: Int){
print("do something...")
}
func add(x:Int,y:Int) -> Int{
return x+y
}
_ = add(x: 1, y: 2)
答案揭晓
Swift里的_
(下划线)是省略符号,代表省略的意思,详细作用可看下方注释:
let _ = 100 //无实际含义
var _ = 200 //无实际含义
//循环三次,省略计数器
for _ in 0...3 {
print("do something...")
}
//去掉参数标签
func fun1( _ x: Int, _ y: Int){
print("do something...")
}
fun1(10,20)
//省略返回值
func add(x:Int,y:Int) -> Int{
return x+y
}
_ = add(x: 1, y: 2)