【发布时间】:2018-10-15 01:27:53
【问题描述】:
我正在编写一个 MacOS 应用程序,它的进程在 Application Support 目录中创建一个临时文件夹,在该文件夹中生成一些新文件,然后还将一些用户指定的文件复制到其中,最后复制完整的我的临时文件夹的内容到用户指定的最终位置。启动应用程序后第一次运行此导出过程时一切正常,但如果多次运行,则会崩溃并出现 EXC_BAD_ACCESS 错误。应用程序生成的文件始终可以正常创建和写入,但当 FileManager 尝试复制现有用户选择的文件之一时,总是会发生崩溃,即使它通过了我的保护语句检查现有文件是否可读且目标路径是可写的。如果你重新启动应用程序,它会再次运行第一次没有问题,但第二次崩溃。
以下是相关代码:
let fm = FileManager.default
guard let existingINIURL = currentExportProfile!.existingINIFileURL else {
return (false, "No INI file location provided.")
}
guard fm.fileExists(atPath: existingINIURL.path), fm.isReadableFile(atPath: existingINIURL.path) else {
return (false, "Could not find INI file at specified path: \(existingINIURL.path) or path is not readable.")
}
guard let outputURL = exportTempFilesURL?.appendingPathComponent("OUTPUT", isDirectory: true), fm.fileExists(atPath: outputURL.path) else {
return (false, "Problem accessing temp staging path")
}
guard fm.isReadableFile(atPath: existingINIURL.path) else {
return (false, "Existing file is not readable")
}
guard fm.isWritableFile(atPath: outputURL.path) else {
return (false, "Destination \(outputURL.path) is not writable")
}
do {
try fm.copyItem(at: existingINIURL, to: outputURL.appendingPathComponent("CONTROL.INI"))
return (true, nil)
} catch let error as NSError {
Swift.print("Failed to copy existing INI with error: \(error)")
return (false, "Failed to copy existing INI file with error: \(error)")
}
EXC_BAD_ACCESS 崩溃总是发生在以下行:
try fm.copyItem(at: existingINIURL, to: outputURL.appendingPathComponent("CONTROL.INI"))
因为它当然是一个访问错误,所以它永远不会到达 catch 语句来给我任何关于问题所在的指示。
有趣的注释:我没有使用.appendingPathComponent("CONTROL.INI") 进行复制,而是尝试使用当前名称复制文件,然后在单独的步骤中重命名复制的文件。当我这样做时,它一开始似乎在工作,但实际上它只是让它工作了 3-4 次,然后以相同的方式崩溃,而不是在第二次尝试时总是以同样的错误崩溃,仍然在 @987654324 @线。
有人遇到过 FileManager 的类似问题吗?
谢谢!
【问题讨论】:
-
崩溃的回溯是什么?
标签: swift macos nsfilemanager