7.10 The guard Statement
The guard statement is a Swift language feature introduced as part of Swift 2. A guard statement contains a Boolean expression which must evaluate to true in order for the code located after the guard statement to be executed. The guard statement must include an else clause to be executed in the event that the expression evaluates to false. The code in the else clause must contain a statement to exit the current code flow (i.e. a return, break, continue or throw statement). Alternatively, the else block may call any other function or method that does not itself return.
The syntax for the guard statement is as follows:
guard <boolean expressions> else {
// code to be executed if expression is false
<exit statement here>
}
// code here is executed if expression is true
The guard statement essentially provides an “early exit” strategy from the current function or loop...