switch高级用法<br><br>
switch+区间运算符<br><br>
switch+元组<br>
switch+下划线 形成模式匹配<br>
switch point {<br>
case (0,_) : print ("it")<br>
case (-2...2,-2...2): print ("")<br>
default:<br>
}<br>
<br>
另外,let point = (8,0)<br>
switch point{<br>
case (0,0):<br>
print("It's origin")<br>
case (let x,0):<br>
print("It's on the x-axis")<br>
print("The X value is \(x)")<br>
case(0,let y):<br>
print("It's on the y-axis")<br>
print("The y value is \(y)")<br>
case(let x,let y):<br>
print("The point is (\(x),\(y)")<br>
}<br>
与下划线不同的是,把 元组某个位置的值赋予 x和 y。有时可能会用到这些值。
如果需要在执行完某个 case 后继续执行后续 case,需要添加 fallthrough。
截图
0赞 · 0采集
口口大爷
2016-07-24
switch 中的 fallthrough 跳转下一个case
截图
0赞 · 0采集
leavie
2016-07-22
statement: if, switch
case keyword: case label, case clause
pattern: case sub pattern 1, sub2
switch expression{
case:sub1, sub2:->label1
/* labe1 -> sub1 == expression || sub2 === expression*/
statement
}
if condition with case clause{/*
case clause1 sub1, sub2 ,
case clause2 sub3, sub4
condition-----> sub1 && sub2 && sub3 && sub4*/
statement
}
0赞 · 0采集
perter
2016-07-19
//区间 Switch
let score = 90
switch score {
case 0:
print("You get a egg")
case 1..<60:
print("You faild")
case 60..<80:
print("You Just soso")
case 80..<100:
print("You Great")
case 100:
print("Perfect")
default:
break
}
//元组 Switch
let vector = (1,1)
switch vector {
case (1,2):
print("------------12")
case (1,4):
print("============11")
case (_,1):
print("============22")
fallthrough
case (-1...1,0...9):
print("============33")
default:
break
}