控制流
1
for in 和c用法一致
特殊:
for tickMark in stride(from: 0, to: 12, by: Interval) {
print(tickMark)
}
for tickMark in stride(from: 0, through: 12, by: Interval) {
print(tickMark)
}
2
范围定义
2.1swift中的switch 不用使用break
2.2如果要向下继续执行,可以使用fallthrough 需注意的是 fallthrough 只能向下执行一次case 且不会做条件判断
2.3如果在控制流内使用控制流(嵌套) 可以为控制流添加标签 例如 gameLoop:while
例子: 元组类型
let person = ("Tony",15)
switch person {
case ("Tony",15):
print("我是Tony")
case (_,15):
print("和tony 一般大")
case ("Tony",_):
print("和tony 同名")
default:
print("null")
}
值绑定
let origin = (2,3)
switch origin {
case (let x , 5):
print("x==\(x)")
case (1 , let y):
print("y==\(y)")
case (let x,let y):
print("x==\(x)==\(y)")
}
结合where使用
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == 1:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}