- 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.
推薦閱讀
- 同步:秩序如何從混沌中涌現(xiàn)
- Python數(shù)據(jù)分析入門:從數(shù)據(jù)獲取到可視化
- DB29forLinux,UNIX,Windows數(shù)據(jù)庫管理認證指南
- 新型數(shù)據(jù)庫系統(tǒng):原理、架構(gòu)與實踐
- 圖解機器學習算法
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- Sybase數(shù)據(jù)庫在UNIX、Windows上的實施和管理
- ZeroMQ
- Oracle PL/SQL實例精解(原書第5版)
- R語言數(shù)據(jù)挖掘
- 探索新型智庫發(fā)展之路:藍迪國際智庫報告·2015(下冊)
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 大數(shù)據(jù)時代系列(套裝9冊)
- 數(shù)據(jù)挖掘與機器學習-WEKA應用技術與實踐(第二版)
- Rust High Performance