Swift心得筆記之掌握流。本站提示廣大學習愛好者:(Swift心得筆記之掌握流)文章只能為提供參考,不一定能成為您想要的結果。以下是Swift心得筆記之掌握流正文
掌握流根本上年夜同小異,在此羅列幾個比擬風趣的處所。
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 弄起:
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”
Control Transfer Statements
Swift 有四個掌握轉移狀況:
continue - 針對 loop ,直接停止下一次輪回迭代。告知輪回體:我此次輪回曾經停止了。
break - 針對 control flow (loop + switch),直接停止全部掌握流。在 loop 中會跳出以後 loop ,在 switch 中是跳出以後 switch 。假如 switch 中某個 case 你其實不想停止任何處置,你可以直接在外面加上 break 來疏忽。
fallthrough - 在 switch 中,將代碼引至下一個 case 而不是默許的跳出 switch。
return - 函數中應用
其他
看到一個風趣的器械:Swift Cheat Sheet,外面是純潔的代碼片斷,假如忽然短路忘了語法可以來看看。
好比 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") }
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。