標籤:使用 strong io for cti re
閉區間運算子
閉區間運算子(a...b)定義一個包含從a到b(包括a和b)的所有值的區間,只能是數字
for index in 1...5 { println("\(index) * 5 = \(index * 5)")}var names = ["Anna", "Alex", "Brian", "Jack"]names[2...3] = ["a","b"] //不能添加
半閉區間
半閉區間(a..b)定義一個從a到b但不包括b的區間。方便取數組下標
let names = ["Anna", "Alex", "Brian", "Jack"]let count = names.countfor i in 0..count { println("第 \(i + 1) 個人叫 \(names[i])")}for i in 0...count-1{ println("第 \(i + 1) 個人叫 \(names[i])")}
閉區間和半閉區間代替了傳統的.for迴圈.使用i++的形式,還可以用於再數組上
fon-in
for-in用來遍曆一個區間(range),序列(sequence),集合(collection),系列(progression)裡面所有的元素執行一系列語句。
//區間for index in 1...5 { println("\(index) times 5 is \(index * 5)")}//數組let names = ["Anna", "Alex", "Brian", "Jack"]for name in names { println("Hello, \(name)!")}//字典let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs")}//字元for character in "Hello" { println(character)}
for-in中index是一個每次迴圈遍曆開始時被自動賦值的常量。這種情況下,index在使用前不需要聲明,只需要將它包含在迴圈的聲明中 ,就可以對其進行隱式聲明,而無需使用let關鍵字聲明。
index常量只存在於迴圈的生命週期裡。如果你想在迴圈完成後訪問index的值,又或者想讓index成為一個變數而不是常量,你必須在迴圈之前自己進行聲明。
for迴圈
在初始設定式中聲明的常量和變數(比如var index = 0)只在for迴圈的生命週期裡有效。如果想在迴圈結束後訪問index的值,你必須要在迴圈生命週期開始前聲明index。
for initialization; condition; increment {statements}
等同於
initializationwhile condition {statementsincrement}
switch
switch當匹配後,不會繼續執行下一個case,會終止switch語句,所以不需要break語句.如果想要貫穿至特定的 case 分支中,請使用fallthrough貫穿語句
每一個 case 分支都必須包含至少一條語句。代替可以使用","匹配多個case,為同一個值
let someCharacter: Character = "e"switch someCharacter {case "a", "e", "i", "o", "u": println("\(someCharacter) is a vowel")case "b", "c", "d",: println("\(someCharacter) is a consonant")default: println("\(someCharacter) is not a vowel or a consonant")}//case 條件可以是區間,尋找一個數字是否在一個區間switch count {case 0: naturalCount = "no"case 1...3: naturalCount = "a few"case 4...9: naturalCount = "several"case 10...99: naturalCount = "tens of"case 100...999: naturalCount = "hundreds of"case 1000...999_999: naturalCount = "thousands of"default: naturalCount = "millions and millions of"}使用元組在同一個switch語句中測試多個值。元組中的元素可以是值,也可以是區間。另外,使用底線(_)來匹配所有可能的值。let somePoint = (1, 1)switch somePoint {case (0, 0): println("(0, 0) is at the origin")case (_, 0): println("(\(somePoint.0), 0) is on the x-axis")case (0, _): println("(0, \(somePoint.1)) is on the y-axis")case (-2...2, -2...2): println("(\(somePoint.0), \(somePoint.1)) is inside the box")default: println("(\(somePoint.0), \(somePoint.1)) is outside of the box")}//值綁定let anotherPoint = (2, 0)switch anotherPoint {case (let x, 0): println("on the x-axis with an x value of \(x)")case (0, let y): println("on the y-axis with a y value of \(y)")case let (x, y): println("somewhere else at (\(x), \(y))")}// 輸出 "on the x-axis with an x value of 2"//使用wherelet 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"
Label
使用break和continue在多重迴圈或者switch嵌套中跳轉
gameLoop: while square != finalSquare { if ++diceRoll == 7 { diceRoll = 1 } switch square + diceRoll { case finalSquare: // 到達最後一個方塊,遊戲結束 break gameLoop case let newSquare where newSquare > finalSquare: // 超出最後一個方塊,再擲一次骰子 continue gameLoop default: // 本次移動有效 square += diceRoll square += board[square] }}println("Game over!")