// *** before: ***
func greetOld(person: [String: String]) {
if let name = person["name"] {
if let city = person["city"] {
print("Hello \(name) from \(city)")
} else {
print("no city")
}
} else {
print("no name")
}
}
// *** in version 2.0: ***
func greet(person: [String: String]) {
guard let name = person["name"] else {
print("no name")
return
}
guard let city = person["city"] else {
print("no city")
return
}
// the values live on until the end of the function, and the nesting is gone
print("Hello \(name) from \(city)")
}
// the else branch must leave the scope: return, break, continue or throw