2
我想從Swift異常處理程序生成一個NSAlert樣式的錯誤表。在與Objective-C的過去,我可以簡單地與雨燕採用[NSAlert alertWithError:]
現在和它自己的異常處理系統中拌勻,警報感到非常笨拙:從Swift異常生成錯誤表
enum Problems: ErrorType {
case Bad
case Worse
}
func f() throws {
throw Problems.Worse
}
func g() {
let errorMessage: String
do {
try f()
}
catch Problems.Bad {
errorMessage = "This was bad"
}
catch Problems.Worse {
errorMessage = "This was even worse"
}
catch {
errorMessage = "This was unexpected"
}
guard errorMessage == nil else {
let alert = NSAlert()
alert.messageText = "Error"
alert.informativeText = errorMessage
alert.beginSheetModalForWindow(self, completionHandler: { [unowned self] (returnCode) -> Void in
if returnCode == NSAlertFirstButtonReturn {
// Handle the response
}
})
}
這種感覺缺憾和一樣,如果它不能很好地有有很多錯誤條件必須檢查。有沒有更好的辦法?