本篇是《Swift語法學習筆記》系列的第二篇文章,將涵蓋以下內容:
- 流程控制
- if語句
- 一般使用方法
- 三目運算符
- 多個條件用逗號隔開
- where詳解
- for循環(huán)
- 一般使用方法
- 自定義遞進序列
- 遍歷
- while語句
- repeat語句
- Switch語句
- 一般使用方法
- 字符串case分支
1 if語句
1.1 一般使用方法
Swift中的if語句必須包含一對花括號邢羔,即使只有一行語句他去。另外,if后緊跟的判斷語句可以不用()括起來柱锹,具體如下所示:
let random = arc4random_uniform(10)
if random < 5 {
print("Hi")
} else {
print("Ho")
}
1.2 三目運算符
1.1小節(jié)的例子可以精簡為:
let random = arc4random_uniform(10)
random < 5 ? print("Hi") : print("Ho")
或者
let random = arc4random_uniform(10)
print(random < 5 ? "Hi" : "Ho")
1.3 多個條件用逗號隔開
let dict = [0: "Red", 1: "Green", 2: "Blue", 3: "Green", 4: "Yellow"]
let color1 = dict[Int(arc4random_uniform(6))]
let color2 = dict[Int(arc4random_uniform(6))]
if let color1 = color1, color2 = color2 {
// Executed only if both colors were not nil
print("color1: \(color1), color2: \(color2)")
}
1.4 where詳解
var optionName: String? = "Ricky"
if let name = optionName where name.hasPrefix("R"){
print("\(name)")
}
例子中的if語句執(zhí)行的是:把optionName的值賦予常量name哪自,如果沒有值將為false退出if。而where執(zhí)行的是判斷可選變量optionName的首字母是否為大寫"R"禁熏,如果為false將退出if壤巷。這兩個語句放在一起時,可以理解為“&&”的條件瞧毙。無論是前面的語句還是后面的where語句胧华,只要其中一個不符合規(guī)則寄症,將輸出false退出if語句體。
2 for循環(huán)
2.1 一般使用方法
for var i = 0; i < 10; i++ {
print(i)
}
或者
for i in 0..<10 {
print(i)
}
在使用in格式的for循環(huán)時矩动,循環(huán)條件的控制變量無需聲明有巧,可以直接使用。
2.2 自定義遞進序列
需要用到“數(shù)字.stride”來產生遞進序列:
1.stride(through: 5, by: 1) // 1,2,3,4,5
1.stride(to: 5, by: 1) // 1,2,3,4
應用到for循環(huán)中:
for i in 10.stride(through: 0, by: -2) {
print(i)
}
2.3 遍歷
遍歷數(shù)組:
let strings = ["A", "B", "C"]
for string in strings {
print(string)
}
遍歷
Set:let strings = Set<String>(["A", "B", "C"])
for string in strings {
print(string)
}
遍歷字典:
let d = [ 0: "Red", 1: "Green", 2: "Blue"]
for key in d.keys {
print("\(key) -> \(d[key])")
}
或者
for (key, value) in d {
print("\(key) -> \(value)")
}
3 while語句
var i = 10
while i > 0 {
print(i--)
}
4 repeat語句
var j = 10
repeat {
print(j--)
} while j > 0
5 Switch語句
5.1 一般使用方法
let value = 11
switch value {
case 2, 3, 5, 7, 11, 13, 17, 19:
print("Count is prime and less than 20")
case 20...30:
print("Count is between 20 and 30")
default:
print("Greater than 30")
}
- 建議使用default悲没,因為假如在case中找不到任何的value的值篮迎,那么將會產生一個運行時錯誤;
-
不允許使用空的case語句示姿。
switch (value) {
case 2:
case 3: // Illegal – previous case is empty.
print("Value is 2 or 3")
default:
print("Value is neither 2 nor 3")
}
上述代碼是錯誤的甜橱,需要修正為:
switch (value) {
case 2, 3: // Correct: catches value 2 and value 3
print("Value is 2 or 3")
default:
print("Value is neither 2 nor 3")
}
或者
switch (value) {
case 2: fallthrough
case 3: // Illegal – previous case is empty.
print("Value is 2 or 3")
default:
print("Value is neither 2 nor 3")
}
5.2 字符串case分支
let s = "Hello"
switch s {
case "Hello":
print("Hello to you, too")
case "Goodbye":
print("See you tomorrow")
default:
print("I don't understand")
}
或者
enum Status {
case OK
case ERROR(String)
}
let result = Status.ERROR("Network connection rejected")
switch (result) {
case .OK:
print("Success!")
case .ERROR(let message):
print("Ooops: \(message)")
}
由于編譯器知道result是一個枚舉類型,所以可以省略Status栈戳,但是不可以省略.岂傲。另外,編譯器也知道這個枚舉類型只有兩個值荧琼,所以可以省略default語句譬胎。
參考文獻
《Beginning iPhone Development with Swift 2 - Exploring the iOS SDK 》From page 777 to page 838.
聯(lián)系作者
- 評論
- xueyublue@gmail.com
- qq905375641