控制流基本上大同小异,在此列举几个比较有趣的地方。
switch
Break
文档原文是 No Implicit Fallthrough ,粗暴的翻译一下就是:不存在隐式贯穿。其中 Implicit 是一个经常出现的词,中文原意是:“含蓄的,暗示的,隐蓄的”。在 Swift 中通常表示默认处理。比如这里的隐式贯穿,就是指传统的多个 case 如果没有 break 就会从上穿到底的情况。再例如 implicitly unwrapped optionals ,隐式解析可选类型,则是默认会进行解包操作不用手动通过 ! 进行解包。
回到 switch 的问题,看下下面这段代码:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
println("The letter a")
case "A":
println("The letter A")
default:
println("Not the letter A")
}
可以看到虽然匹配到了 case "a" 的情况,但是在当前 case 结束之后便直接跳出,没有继续往下执行。如果想继续贯穿到下面的 case 可以通过 fallthrough 实现。
Tuple
我们可以在 switch 中使用元祖 (tuple) 进行匹配。用 _ 表示所有值。比如下面这个例子,判断坐标属于什么区域:
let somePoint = (1, 1)
switch somePoint {
case (0, 0): // 位于远点
println("(0, 0) is at the origin")
case (_, 0): // x为任意值,y为0,即在 X 轴上
println("((somePoint.0), 0) is on the x-axis")
case (0, _): // y为任意值,x为0,即在 Y 轴上
println("(0, (somePoint.1)) is on the y-axis")
case (-2...2, -2...2): // 在以原点为中心,边长为4的正方形内。
println("((somePoint.0), (somePoint.1)) is inside the box")
default:
println("((somePoint.0), (somePoint.1)) is outside of the box")
}
// "(1, 1) is inside the box"
如果想在 case 中用这个值,那么可以用过值绑定 (value bindings) 解决:
let somePoint = (0, 1)
switch somePoint {
case (0, 0):
println("(0, 0) is at the origin")
case (let x, 0):
println("x is (x)")
case (0, let y):
println("y is (y)")
default:
println("default")
}
Where
case 中可以通过 where 对参数进行匹配。比如我们想打印 y=x 或者 y=-x这种45度仰望的情况,以前是通过 if 解决,现在可以用 switch 搞起:








