我一直想这样做:
do {
let result = try getAThing()
} catch {
//error
}
do {
let anotherResult = try getAnotherThing(result) //Error - result out of scope
} catch {
//error
}
但似乎只能这样做:
do {
let result = try getAThing()
do {
let anotherResult = try getAnotherThing(result)
} catch {
//error
}
} catch {
//error
}
有没有办法在范围内保持不可变result
而不必嵌套 do/catch 块?有没有一种方法可以防止错误,类似于我们如何将guard
语句用作 if/else 块的反转?