A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
guard
is a conditional statement that requires execution to exit the current block if the condition isn’t met.
Syntax:
guard <condition> else {
<statements>
}
The value of any condition in a guard statement must have a type that conforms to the BooleanType protocol, it can also be an optional binding declaration.
Any constants or variables assigned a value from an optional binding declaration in a guard statement condition can be used for the rest of the guard statement’s enclosing scope.
For example:
guard let value = someOptional else {
// someOptional is nil, value is not accessible
return
}
// do something with the value
// value is available here