guard 是Swift 2.0引入的新关键字, 用于当条件不满足时, 不再执行后续的代码. 当然, 官方文档上说是交出当前作用域的程序控制权.

guard 的格式是这样的:

guard =condition= else {
    =statements=
}

一个例子🌰:

let hello = false

guard hello else {
    fatalError("got error.")
}

guard 不能滥用, else 里面的语句不宜太复杂. 应当条件不满足后, 马上退出或者抛出异常.

可以继续参看 Swift 官方语言手册上, 关于 Guard Statement 一节.

https://docs.swift.org/swift-book/ReferenceManual/Statements.html