Guard - RaduG/swift_learning GitHub Wiki
A condition must be true for the guard statement to execute.
func greet(person: Person) {
guard let name = person.name else {
return
}
guard let location = person.location else {
return
}
print("All good")
}
This is equivalent to:
func greet(person: Person) {
if let name = person.name {
if let location = person.location {
print("All good")
} else {
return
}
} else {
return
}
}
The else block of a guard statement must exit the current execution context (using return, break etc).