Swift 語言支持C語言所有的控制語句,包括for 和while循環語句,if和switch條件語句,以及break和continue控制語句等。
Swift 語言除了支持以上語句,還增加了一個for-in循環語句,來更方面地遍歷數組、詞典、範圍、字符串和其它序列等。
1、for-in循環
for index in 1...5 {
println("\(index) times 5 is \(index *5)")
}
以上for-in循環用來遍歷一個閉合的的範圍。
爲了語句的簡潔,以上語句中使用到的index可以從循環執行體中推斷出是一個常量,因此該常量不需要在使用之前使用let 關鍵字來聲明。如果想使用它作爲變量,就必須對它進行聲明。
在循環語句或條件語句中聲明的常量或變量僅在循環執行體或循環執行體中有效。
如果不需要使用for-in範圍的每一個值,以上語句還可以採用如下形式:
let base =3
let power =10
var answer =1
for _in 1...power {
answer *=base
}
2 Switch語句
Swift 語言對switch語法進行了優化,功能做了增強。
優化後的switch 語法更加安全、語義更加清楚。
Swift 語言要求switch的每個分支必須是全面的,即switch聲明的每一個可能的值必須執行Case分支之一。如果每個Case分支已經全面覆蓋了每種情況,default 分支語句就可以省去。
Swift 語言不允許帶有空執行體的Case分支, 每個Case執行體必須對應一個Case分支,但多個可能的值可以放到一個Case聲明中,用於對應一個執行分支,一個Case分支的多個匹配值由逗號分割,對應一個Case分支的多個匹配值可以分成多個行顯示。
Swift 語言不需要在每個Case分支添加一個多餘的break語句,Swift 語言執行完一個Case分支的執行體後,自動退出switch語句,這樣可以避免C語言經常出現的由於缺少break語句引起的邏輯錯誤。
Swift 語言支持使用Fallthrough語句來明確說明在一個Case執行體執行完後不退出switch語句而是直接執行接着的Case執行體或者default執行體。
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")
}
由於Swift 語言要求每個Case分支必須包含一個至少一條語句的執行體。如下代碼由於第一個case分支 缺少執行體將報一個編譯錯誤,該優化也從語法上避免了一個case執行另外的case的情況,語法也更清晰。
let anotherCharacter:Character ="a"
switch anotherCharacter {
case "a":
case "A":
println("The letter A")
default:
println("Not the letter A")
}
Swift 語言 的switch Case 分支可以採用許多不同類型的匹配模式,包括範圍、多元組。
如下是一個使用多元組匹配的例子。
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")
}
Swift 語言允許多個case 分支符合某個條件,在某個值匹配多個case 分支的情況下, Swift 語言規定總是使用第一個最先匹配的分支。如以上例子雖然(0, 0)點匹配所有四個case 分支,但它只執行首先匹配到的分支case (0,0)對應的執行體,其它匹配的分支將被忽略。
以下是一個使用範圍進行匹配的例子。
let count =3_000
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"
}
在Case分支中,匹配值還能被綁定到一個臨時常量或變量,以便在也只能在Case的執行體中使用。
如下是一個使用值綁定的例子。
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))")
}
每一個Case分支還能使用where 從句來檢查額外的更加複雜的條件。如下所示:
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")
}
3、傳輸控制語句和標籤語句
Swift 除了支持標準的continue、break、return傳輸控制語句外,還提供了一個以上提到的fallthrough傳輸控制語句。
Swift 也支持循環語句或switch語句的嵌套,另外Swift 還支持爲一個循環語句或switch語句添加一個標籤,然後可以使用傳輸控制語句continue、break跳轉到該標籤語句處執行。如下所示:
label name:while condition {
switch condition {
case value 1:
statements 1
break label name
case value 2:
statements 2
continue label name
}
}
版權所有,請轉載時清楚註明鏈接和出處!