- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 130字
- 2021-07-02 23:54:26
Guard
A guard statement can be used for early exits. We can use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. The following example presents the guard statement usage:
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello Ms\(name)!")
}
In this example, the greet function requires a value for a person's name; therefore, it checks whether it is present with the guard statement, otherwise it will return and not continue to execute. As can be seen from the example, the scope of the guarded variable is not only the guard code block, so we were able to use name after the guard code block in our print statement.
推薦閱讀
- LibGDX Game Development Essentials
- Voice Application Development for Android
- Libgdx Cross/platform Game Development Cookbook
- MySQL從入門到精通(第3版)
- 數據要素五論:信息、權屬、價值、安全、交易
- Python數據分析:基于Plotly的動態可視化繪圖
- Proxmox VE超融合集群實踐真傳
- 大數據技術入門
- Augmented Reality using Appcelerator Titanium Starter
- 區塊鏈技術應用與實踐案例
- 數據修復技術與典型實例實戰詳解(第2版)
- 計算機視覺
- Managing Software Requirements the Agile Way
- 實用數據結構基礎(第四版)
- Reactive Programming in Kotlin