【发布时间】:2020-04-23 16:00:00
【问题描述】:
我想将 MIDI 文件保存到某个文件夹。但不幸的是,只是得到一个“无标题”的 txt 文件。
我找到了我尝试过的这段代码:
let savePanel = NSSavePanel()
let bundleFile = Bundle.main.url(forResource: "Melody", withExtension: "mid")!
// this is a preferred method to get the desktop URL
savePanel.directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
savePanel.message = "My custom message."
savePanel.nameFieldStringValue = "MyFile"
savePanel.showsHiddenFiles = false
savePanel.showsTagField = false
savePanel.canCreateDirectories = true
savePanel.allowsOtherFileTypes = false
savePanel.isExtensionHidden = false
if let url = savePanel.url, savePanel.runModal() == NSApplication.ModalResponse.OK {
print("Now copying", bundleFile.path, "to", url.path)
// Do the actual copy:
do {
try FileManager().copyItem(at: bundleFile, to: url)
} catch {
print(error.localizedDescription)
} else {
print("canceled")
}
我可以改进什么以将 MIDI 文件从应用程序包复制到例如桌面??
谢谢!
【问题讨论】:
标签: swift macos cocoa nsbundle