// *** 以前 ***
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")
}
}
// *** 在 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
}
// 值一直存活到函数结束,嵌套也消失了
print("Hello \(name) from \(city)")
}
// else 分支必须离开当前作用域,方式是 return、break、continue 或 throw 之一