For循環(huán)
for循環(huán)用來按照指定的次數(shù)多次執(zhí)行一系列語句。Swift 提供兩種for循環(huán)形式:
-
for-in用來遍歷一個(gè)區(qū)間(range),序列(sequence),集合(collection),系列(progression)里面所有的元素執(zhí)行一系列語句。
1.區(qū)間
<pre><code>for index in 1...5 {
println("(index) times 5 is (index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
</code></pre>
2.區(qū)間(忽略對值的訪問)
<pre><code>let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
println("(base) to the power of (power) is (answer)")
// 輸出 "3 to the power of 10 is 59049"
</code></pre>
這個(gè)計(jì)算并不需要知道每一次循環(huán)中計(jì)數(shù)器具體的值,只需要執(zhí)行了正確的循環(huán)次數(shù)即可。下劃線符號(hào)_(替代循環(huán)中的變量)能夠忽略具體的值,并且不提供循環(huán)遍歷時(shí)對值的訪問。
3.遍歷數(shù)組
<pre><code>let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
println("Hello, (name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!</code></pre>
4.遍歷字典
<pre><code>
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
println("(animalName)s have (legCount) legs")
}
// spiders have 8 legs
// ants have 6 legs
// cats have 4 legs
</code></pre>
字典元素的遍歷順序和插入順序可能不同,字典的內(nèi)容在內(nèi)部是無序的,所以遍歷元素時(shí)不能保證順序。
5.遍歷字符
<pre><code>for character in "Hello" {
println(character)
}
// H
// e
// l
// l
// o
</code></pre>
-
for條件遞增(for-condition-increment)語句,用來重復(fù)執(zhí)行一系列語句直到達(dá)成特定條件達(dá)成,一般通過在每次循環(huán)完成后增加計(jì)數(shù)器的值來實(shí)現(xiàn)。
1.for initialization; condition; increment { statements }
<pre><code>for var index = 0; index < 3; ++index {
println("index is (index)")
}
// index is 0
// index is 1
// index is 2
</code></pre>
Switch
switch語句會(huì)嘗試把某個(gè)值與若干個(gè)模式(pattern)進(jìn)行匹配。根據(jù)第一個(gè)匹配成功的模式,switch語句會(huì)執(zhí)行對應(yīng)的代碼。當(dāng)有可能的情況較多時(shí),通常用switch語句替換if語句。
- 正常switch
<pre><code>let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
println("(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
println("(someCharacter) is a consonant")
default:
println("(someCharacter) is not a vowel or a consonant")
}
// 輸出 "e is a vowel"
</code></pre>一個(gè) case 也可以包含多個(gè)模式,用逗號(hào)把它們分開(如果太長了也可以分行寫)
<pre><code>switch some value to consider { case value 1, value 2: statements }</code></pre>
不存在隱式的貫穿(No Implicit Fallthrough)
在 Swift 中,當(dāng)匹配的 case 分支中的代碼執(zhí)行完畢后,程序會(huì)終止switch語句,而不會(huì)繼續(xù)執(zhí)行下一個(gè) case 分支。不需要在 case 分支中顯式地使用break語句
區(qū)間匹配(Range Matching)
case 分支的模式也可以是一個(gè)值的區(qū)間。下面的例子展示了如何使用區(qū)間匹配來輸出任意數(shù)字對應(yīng)的自然語言格式:
<pre><code>
let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
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"
}
println("There are (naturalCount) (countedThings).")
// 輸出 "There are millions and millions of stars in the Milky Way."</code></pre>匹配元組(Tuple)
你可以使用元組在同一個(gè)switch語句中測試多個(gè)值。元組中的元素可以是值,也可以是區(qū)間。另外,使用下劃線()來匹配所有可能的值。
<pre><code>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")
}
// 輸出 "(1, 1) is inside the box"</code></pre>值綁定(Value Bindings)
case 分支的模式允許將匹配的值綁定到一個(gè)臨時(shí)的常量或變量,這些常量或變量在該 case 分支里就可以被引用了——這種行為被稱為值綁定(value binding)。
<pre><code>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"</code></pre>
- Where語句
case 分支的模式可以使用where語句來判斷額外的條件。
<pre><code>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"</code></pre>
控制轉(zhuǎn)移語句(Control Transfer Statements)
控制轉(zhuǎn)移語句改變你代碼的執(zhí)行順序,通過它你可以實(shí)現(xiàn)代碼的跳轉(zhuǎn)。Swift有四種控制轉(zhuǎn)移語句。
-
continue
continue語句告訴一個(gè)循環(huán)體立刻停止本次循環(huán)迭代,重新開始下次循環(huán)迭代。就好像在說“本次循環(huán)迭代我已經(jīng)執(zhí)行完了”,但是并不會(huì)離開整個(gè)循環(huán)體。
注意:
在一個(gè)for條件遞增(for-condition-increment)循環(huán)體中,在調(diào)用continue語句后,迭代增量仍然會(huì)被計(jì)算求值。循環(huán)體繼續(xù)像往常一樣工作,僅僅只是循環(huán)體中的執(zhí)行代碼會(huì)被跳過。
<pre><code>let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput {
switch character {
case "a", "e", "i", "o", "u", " ":
continue
default:
puzzleOutput.append(character)
}
}
println(puzzleOutput)
// 輸出 "grtmndsthnklk"</code></pre>
在上面的代碼中,只要匹配到元音字母或者空格字符,就調(diào)用continue語句,使本次循環(huán)迭代結(jié)束,從新開始下次循環(huán)迭代。這種行為使switch匹配到元音字母和空格字符時(shí)不做處理,而不是讓每一個(gè)匹配到的字符都被打印。
-
break
break語句會(huì)立刻結(jié)束整個(gè)控制流的執(zhí)行。當(dāng)你想要更早的結(jié)束一個(gè)switch代碼塊或者一個(gè)循環(huán)體時(shí),你都可以使用break語句。
1.循環(huán)語句中的 break
當(dāng)在一個(gè)循環(huán)體中使用break時(shí),會(huì)立刻中斷該循環(huán)體的執(zhí)行,然后跳轉(zhuǎn)到表示循環(huán)體結(jié)束的大括號(hào)(})后的第一行代碼。不會(huì)再有本次循環(huán)迭代的代碼被執(zhí)行,也不會(huì)再有下次的循環(huán)迭代產(chǎn)生。
2.Switch 語句中的 break
當(dāng)在一個(gè)switch代碼塊中使用break時(shí),會(huì)立即中斷該switch代碼塊的執(zhí)行,并且跳轉(zhuǎn)到表示switch代碼塊結(jié)束的大括號(hào)(})后的第一行代碼
-
fallthrough
Swift 中的switch不會(huì)從上一個(gè) case 分支落入到下一個(gè) case 分支中。相反,只要第一個(gè)匹配到的 case 分支完成了它需要執(zhí)行的語句,整個(gè)switch代碼塊完成了它的執(zhí)行
如果你確實(shí)需要 C 風(fēng)格的貫穿(fallthrough)的特性,你可以在每個(gè)需要該特性的 case 分支中使用fallthrough關(guān)鍵字
<pre><code>let integerToDescribe = 5
var description = "The number (integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
println(description)
// 輸出 "The number 5 is a prime number, and also an integer."</code></pre>
注意:
fallthrough關(guān)鍵字不會(huì)檢查它下一個(gè)將會(huì)落入執(zhí)行的 case 中的匹配條件。fallthrough簡單地使代碼執(zhí)行繼續(xù)連接到下一個(gè)case 中的執(zhí)行代碼,這和 C 語言標(biāo)準(zhǔn)中的switch語句特性是一樣的。
-
帶標(biāo)簽的語句(Labeled Statements)
在 Swift 中,你可以在循環(huán)體和switch代碼塊中嵌套循環(huán)體和switch代碼塊來創(chuàng)造復(fù)雜的控制流結(jié)構(gòu)。然而,循環(huán)體和switch代碼塊兩者都可以使用break語句來提前結(jié)束整個(gè)方法體。因此,顯示地指明break語句想要終止的是哪個(gè)循環(huán)體或者switch代碼塊,會(huì)很有用。類似地,如果你有許多嵌套的循環(huán)體,顯示指明continue語句想要影響哪一個(gè)循環(huán)體也會(huì)非常有用。
為了實(shí)現(xiàn)這個(gè)目的,你可以使用標(biāo)簽來標(biāo)記一個(gè)循環(huán)體或者switch代碼塊,當(dāng)使用break或者continue時(shí),帶上這個(gè)標(biāo)簽,可以控制該標(biāo)簽代表對象的中斷或者執(zhí)行。
產(chǎn)生一個(gè)帶標(biāo)簽的語句是通過在該語句的關(guān)鍵詞的同一行前面放置一個(gè)標(biāo)簽,并且該標(biāo)簽后面還需帶著一個(gè)冒號(hào)。下面是一個(gè)while循環(huán)體的語法,同樣的規(guī)則適用于所有的循環(huán)體和switch代碼塊。
<pre><code>label name: while condition { statements }</code></pre>
下面的例子是在一個(gè)帶有標(biāo)簽的while循環(huán)體中調(diào)用break和continue語句,該循環(huán)體是前面章節(jié)中蛇和梯子的改編版本。這次,游戲增加了一條額外的規(guī)則:
為了獲勝,你必須剛好落在第 25 個(gè)方塊中。
如果某次擲骰子使你的移動(dòng)超出第 25 個(gè)方塊,你必須重新擲骰子,直到你擲出的骰子數(shù)剛好使你能落在第 25 個(gè)方塊中。
<pre><code>
let finalSquare = 25
var board = [Int](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0</code></pre>
這個(gè)版本的游戲使用while循環(huán)體和switch方法塊來實(shí)現(xiàn)游戲的邏輯。while循環(huán)體有一個(gè)標(biāo)簽名gameLoop,來表明它是蛇與梯子的主循環(huán)。
該while循環(huán)體的條件判斷語句是while square !=finalSquare,這表明你必須剛好落在方格25中。
<pre><code>
gameLoop: while square != finalSquare {
if ++diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// 到達(dá)最后一個(gè)方塊,游戲結(jié)束
break gameLoop
case let newSquare where newSquare > finalSquare:
// 超出最后一個(gè)方塊,再擲一次骰子
continue gameLoop
default:
// 本次移動(dòng)有效
square += diceRoll
square += board[square]
}
}
println("Game over!")</code></pre>
每次循環(huán)迭代開始時(shí)擲骰子。與之前玩家擲完骰子就立即移動(dòng)不同,這里使用了switch來考慮每次移動(dòng)可能產(chǎn)生的結(jié)果,從而決定玩家本次是否能夠移動(dòng)。
1.如果骰子數(shù)剛好使玩家移動(dòng)到最終的方格里,游戲結(jié)束。break gameLoop語句跳轉(zhuǎn)控制去執(zhí)行while循環(huán)體后的第一行代碼,游戲結(jié)束。
2.如果骰子數(shù)將會(huì)使玩家的移動(dòng)超出最后的方格,那么這種移動(dòng)是不合法的,玩家需要重新擲骰子。continue gameLoop語句結(jié)束本次while循環(huán)的迭代,開始下一次循環(huán)迭代。
3.在剩余的所有情況中,骰子數(shù)產(chǎn)生的都是合法的移動(dòng)。玩家向前移動(dòng)骰子數(shù)個(gè)方格,然后游戲邏輯再處理玩家當(dāng)前是否處于蛇頭或者梯子的底部。本次循環(huán)迭代結(jié)束,控制跳轉(zhuǎn)到while循環(huán)體的條件判斷語句處,再?zèng)Q定是否能夠繼續(xù)執(zhí)行下次循環(huán)迭代。
注意:
如果上述的break語句沒有使用gameLoop標(biāo)簽,那么它將會(huì)中斷switch代碼塊而不是while循環(huán)體。使用gameLoop標(biāo)簽清晰的表明了break想要中斷的是哪個(gè)代碼塊。 同時(shí)請注意,當(dāng)調(diào)用continue gameLoop去跳轉(zhuǎn)到下一次循環(huán)迭代時(shí),這里使用gameLoop標(biāo)簽并不是嚴(yán)格必須的。因?yàn)樵谶@個(gè)游戲中,只有一個(gè)循環(huán)體,所以continue語句會(huì)影響到哪個(gè)循環(huán)體是沒有歧義的。然而,continue語句使用gameLoop標(biāo)簽也是沒有危害的。這樣做符合標(biāo)簽的使用規(guī)則,同時(shí)參照旁邊的break gameLoop,能夠使游戲的邏輯更加清晰和易于理解。