当此 URL 扩展作为面板运行时,它无法返回 URL,似乎绕过了完成:
func saveAs() -> URL? {
let savePanel = NSSavePanel()
var saveAsURL : URL? = nil
savePanel.canCreateDirectories = true
savePanel.nameFieldStringValue = self.lastPathComponent
savePanel.directoryURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
if let keyWindow = NSApp.keyWindow {
savePanel.beginSheetModal(for: keyWindow, completionHandler: { result in
/*if result == .OK {*/ saveAsURL = savePanel.url //}
})
}
else
{
NSApp.activate(ignoringOtherApps: true)
if savePanel.runModal() == .OK {
saveAsURL = savePanel.url
}
}
Swift.print("saveAsURL => \(saveAsURL.debugDescription)")
return saveAsURL
}
但作为独立窗口运行它工作正常。这里有另一个相关的答案,但这里的用法不同:即
guard let saveAsURL = URL.init(string: "download.dmg").saveAs() else { return }
我认为用户取消会推断处理应该结束。