目錄:
- if
- guard
- switch
- 控制轉(zhuǎn)移語句
- 帶標(biāo)簽語句
- 檢測API可用性
1. if
同OC一樣饼酿,只不過條件體不需要括號括起來
let a = 0
if a > 0 {
} else if a == 0 {
} else {
}
2. guard
此語法最好的地兒就是''提前退出''
一個guard語句總有一個else從句,如果條件不為真稚晚,則執(zhí)行else從句代碼.若條件為真在抛,則執(zhí)行g(shù)uard大括號后面的代碼
注意:
如果條件不被滿足,在else分支上的代碼就會被執(zhí)行眉踱。這個分支必須轉(zhuǎn)移控制以退出guard語句出現(xiàn)的代碼段。它可以用控制轉(zhuǎn)移語句如return,break,continue或者throw做這件事,或者調(diào)用一個不返回的方法或函數(shù)脚粟,例如fatalError()
class ViewController: UIViewController {
enum Sections: Int {
case student = 0
case teacher
case dog
}
override func viewDidLoad() {
super.viewDidLoad()
let section = 2
guard Sections.dog.rawValue == section else {
print("條件不為真,走這里了")
return
}
print("條件為真蘸朋,走這里了")
}
}
按需使用guard,會提高我們代碼的可讀性核无,所以,要巧用
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
buy(product: ["name":"衣服"])
/*輸出
我想買: 衣服
在什么位置買買買
*/
buy(product: ["name":"衣服","location" : "南京西路"])
/*輸出
我想買: 衣服
我去 南京西路 買 衣服
*/
}
private func buy(product:[String : String]) {
guard let name = product["name"] else {
return
}
print("我想買:",name)
guard let location = product["location"] else {
print("在什么位置買買買")
return
}
print("我去",location,"買",name)
}
}
3. switch
當(dāng)有可能有較多時藕坯,我們用switch語句团南。通常與枚舉關(guān)聯(lián)較多,我們可以把條件作為枚舉值炼彪,然后用switch進行判斷吐根,這樣可讀性很高.
- Swift 與Int型枚舉值結(jié)合使用演示
class ViewController: UIViewController {
enum Sections: Int {
case student
case teacher
}
override func viewDidLoad() {
super.viewDidLoad()
// 不存在隱士貫穿,所以不需要在case 后加break
let section = 0
switch section {
case Sections.student.rawValue:
print("嘿辐马,student拷橘,section = ", Sections.student.rawValue)
case Sections.teacher.rawValue:
print("嘿,eacher,section = ", Sections.teacher.rawValue)
default: break
}
/* 輸出
嘿冗疮,tudent萄唇,section = 0
*/
}
}
- 復(fù)合匹配
Swift與Int型枚舉值結(jié)合使用演示(將兩個值組合成一個匹配),就是在兩個case間加一個,
即可
class ViewController: UIViewController {
enum Sections: Int {
case student = 0
case teacher
case dog
}
override func viewDidLoad() {
super.viewDidLoad()
let section = 2
switch section {
case Sections.student.rawValue, Sections.dog.rawValue:
if Sections.student.rawValue == section {
print("嘿,Student术幔,section = ",Sections.student.rawValue)
} else {
print("嘿另萤,Dog,section = ",Sections.dog.rawValue)
}
case Sections.teacher.rawValue:
print("嘿诅挑,teacher四敞,section = ",Sections.teacher.rawValue)
default: break
}
/* 輸出
嘿,Dog拔妥,section = 2
*/
}
}
- 牛點來了啊忿危,Swift與元祖結(jié)合使用.列害了world哥
// 視頻需要Id,直播不需要
enum Type {
case video(id: Int)
case live
}
class TwoViewController: UIViewController {
convenience init(type: Type) {
self.init()
switch type {
case Type.video(id: let videoId):
print("當(dāng)類型為視頻,做些視頻該做的事兒",videoId)
case Type.live:
print("當(dāng)=類型為直播毒嫡,做些直播該做的事兒")
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
- 記得適當(dāng)?shù)臅r候我們也可以用
where
(結(jié)合坐標(biāo)圖癌蚁,看代碼)
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
// 輸出 "(1, -1) is on the line x == -y
坐標(biāo)圖
4.控制轉(zhuǎn)移語句
- continue
停止本次循環(huán),立即執(zhí)行下次循環(huán) - break
立刻結(jié)束整個控制流的執(zhí)行兜畸。當(dāng)在循環(huán)體中時努释,執(zhí)行跳出循環(huán)體,停止循環(huán)咬摇。當(dāng)在switch語句中時伐蒂,執(zhí)行跳出switch條件判斷。 - fallthrough
fallthrough,讓 case 之后的語句會按順序繼續(xù)運行肛鹏,且不論條件是否滿足都會執(zhí)行逸邦。 - return
終斷代碼繼續(xù)往下執(zhí)行,這個函數(shù)會立刻返回. - throw
throw來拋出一個錯誤并使用throws來表示一個可以拋出錯誤的函數(shù)。如果在函數(shù)中拋出一個錯誤在扰,這個函數(shù)會立刻返回并且調(diào)用該函數(shù)的代碼會進行錯誤處理(我會自Swift-錯誤處理中介紹)
5.帶標(biāo)簽語句
label name: while condition { statements }
while循環(huán)有一個標(biāo)簽名"gameLoop"代表游戲循環(huán)的主循環(huán)
gameLoop: while square != finalSquare {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// 骰子數(shù)剛好使玩家移動到最終的方格里缕减,游戲結(jié)束。
break gameLoop
case let newSquare where newSquare > finalSquare:
// 骰子數(shù)將會使玩家的移動超出最后的方格芒珠,那么這種移動是不合法的桥狡,玩家需要重新擲骰子
continue gameLoop
default:
// 合法移動,做正常的處理
square += diceRoll
square += board[square]
}
}
6.檢測API可用性
最后一個參數(shù)皱卓,*裹芝,是必須的,用于指定在所有其它平臺中娜汁,如果版本號高于你的設(shè)備指定的最低版本嫂易,if語句的代碼塊將會運行。
if #available(platform name version, ..., *) {
APIs 可用掐禁,語句將執(zhí)行
} else { APIs 不可用怜械,語句將不執(zhí)行
}
```swift
if #available(iOS 10, macOS 10.12, *) {
// 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API
} else {
// 使用先前版本的 iOS 和 macOS 的 API
}