【发布时间】:2020-08-27 17:36:43
【问题描述】:
我在某些视图中有一个按钮,它调用 ViewModel 中可能引发错误的函数。
Button(action: {
do {
try self.taskViewModel.createInstance(name: self.name)
} catch DatabaseError.CanNotBeScheduled {
Alert(title: Text("Can't be scheduled"), message: Text("Try changing the name"), dismissButton: .default(Text("OK")))
}
}) {
Text("Save")
}
try-catch 块产生以下错误:
Invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '() -> Void'
这是 viewModel 中的 createInstance 函数,taskModel 函数以完全相同的方式处理错误。
func createIntance(name: String) throws {
do {
try taskModel.createInstance(name: name)
} catch {
throw DatabaseError.CanNotBeScheduled
}
}
如何正确捕捉 SwiftUI 中的错误?
【问题讨论】:
标签: ios swiftui try-catch throw