控制流基本上大同小异,在此列举几个比较有趣的地方。html
文档原文是 No Implicit Fallthrough
,粗暴的翻译一下就是:不存在隐式贯穿。其中 Implicit
是一个常常出现的词,中文原意是:“含蓄的,暗示的,隐蓄的”。在 Swift 中一般表示默认处理。好比这里的隐式贯穿,就是指传统的多个 case
若是没有 break
就会从上穿到底的状况。再例如 implicitly unwrapped optionals
,隐式解析可选类型,则是默认会进行解包操做不用手动经过 !
进行解包。ios
回到 switch
的问题,看下下面这段代码:git
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
实现。swift
咱们能够在 switch 中使用元祖 (tuple) 进行匹配。用 _
表示全部值。好比下面这个例子,判断坐标属于什么区域:app
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) 解决:ide
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") }
case 中能够经过 where 对参数进行匹配。好比咱们想打印 y=x 或者 y=-x这种45度仰望的状况,之前是经过 if 解决,如今能够用 switch 搞起:函数
let yetAnotherPoint = (1, -1) switch yetAnotherPoint { case let (x, y) where x == y: println("(\(x), \(y)) is on the line x == y") case let (x, y) where x == -y: println("(\(x), \(y)) is on the line x == -y") case let (x, y): println("(\(x), \(y)) is just some arbitrary point") } // "(1, -1) is on the line x == -y”
Swift 有四个控制转移状态:oop
看到一个有趣的东西:Swift Cheat Sheet,里面是纯粹的代码片断,若是忽然短路忘了语法能够来看看。ui
好比 Control Flow 部分,有以下代码,基本覆盖了全部的点:翻译
// for loop (array) let myArray = [1, 1, 2, 3, 5] for value in myArray { if value == 1 { println("One!") } else { println("Not one!") } } // for loop (dictionary) var dict = [ "name": "Steve Jobs", "title": "CEO", "company": "Apple" ] for (key, value) in dict { println("\(key): \(value)") } // for loop (range) for i in -1...1 { // [-1, 0, 1] println(i) } // use .. to exclude the last number // for loop (ignoring the current value of the range on each iteration of the loop) for _ in 1...3 { // Do something three times. } // while loop var i = 1 while i < 1000 { i *= 2 } // do-while loop do { println("hello") } while 1 == 2 // Switch let vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins and make ants on a log." case "cucumber", "watercress": let vegetableComment = "That would make a good tea sandwich." case let x where x.hasSuffix("pepper"): let vegetableComment = "Is it a spicy \(x)?" default: // required (in order to cover all possible input) let vegetableComment = "Everything tastes good in soup." } // Switch to validate plist content let city:Dictionary<String, AnyObject> = [ "name" : "Qingdao", "population" : 2_721_000, "abbr" : "QD" ] switch (city["name"], city["population"], city["abbr"]) { case (.Some(let cityName as NSString), .Some(let pop as NSNumber), .Some(let abbr as NSString)) where abbr.length == 2: println("City Name: \(cityName) | Abbr.:\(abbr) Population: \(pop)") default: println("Not a valid city") }