【发布时间】:2017-07-02 02:52:19
【问题描述】:
我正在尝试使用 UIAlertController 提供一种处理错误的好方法,该控制器可以选择重试导致错误的代码。我想出了这个巨大的混乱,不知何故实际上有效:
func handleError(_ closure: @escaping () throws -> Void) {
do {
try closure()
} catch {
print(error)
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let retry = UIAlertAction(title: "Retry", style: .default, handler: {(_: UIAlertAction) in self.handleError(closure)})
alert.addAction(retry)
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(cancel)
present(alert, animated: true)
}
}
但是,单看这一点,似乎它会导致比阻止错误更多的错误。我能做些什么来使这个简单或不那么混乱吗?我不明白为什么我需要 @escaping 部分(但编译器告诉我需要),或者为什么我需要为 UIAlertAction 闭包提供一个空白参数。
我只是需要一些保证,这不是一个太糟糕的解决方案。
【问题讨论】:
标签: ios swift error-handling uialertcontroller