gpt4 book ai didi

swift - 在 beginSheetModalForWindow 中尝试

转载 作者:可可西里 更新时间:2023-11-01 02:21:12 26 4
gpt4 key购买 nike

在 Swift 2.0 中,如果我执行以下操作:

panel.beginSheetModalForWindow(self.view.window!) { (result) in

switch result {
// Save was selected
case NSFileHandlingPanelOKButton:
self.writeFile(fileString, fileURL: panel.URL!)
// something else was selected
default:
break
}
}

使用如下函数,它编译并运行。

  func writeFile(fileString: String, fileURL: NSURL) {
do {
try fileString.writeToURL(fileURL, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
fatalError("Error writing to file - \(error)")
}
}

但是,如果我像这样将两者结合起来:

panel.beginSheetModalForWindow(self.view.window!) { (result) in
switch result {
// Save was selected
case NSFileHandlingPanelOKButton:
do {
try fileString.writeToURL(fileURL, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
fatalError("Error writing to file - \(error)")
}
// something else was selected
default:
break
}
}

它不会编译。我收到一个编译器错误:

从“() throws ->”类型的抛出函数到非抛出函数类型“(Int) -> Void”的无效转换

为什么?

最佳答案

像这样添加默认的 catch block :

do {
try fileString.writeToURL(fileURL, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
fatalError("Error writing to file - \(error)")
} catch {

}

http://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch

关于swift - 在 beginSheetModalForWindow 中尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30828779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com