【发布时间】:2019-02-17 02:47:53
【问题描述】:
我正在开发一个用于编辑文件的 macOS 应用程序,但在尝试使用 NSDocumentController.shared.makeDocument 从文件 URL 创建新的 NSDocument 实例时遇到了一个相当烦人的错误。
以下是我如何调用makeDocument 的简单示例。文件test.md 存在磁盘上。
let url = URL(fileURLWithPath: "/Users/me/Desktop/test.md"
do {
let newDocument = try NSDocumentController.shared.makeDocument(withContentsOf: url, ofType: url.pathExtension)
print("Created \(newDocument)")
} catch {
print("Error: \(error)")
}
问题是这个try 调用失败并到达catch 块。我得到的错误是:
错误:错误域=NSCocoaErrorDomain 代码=256 “无法处理“test.md”,因为 MyApp 无法打开“md”格式的文件。” UserInfo={NSLocalizedDescription=“test.md” 无法处理,因为 MyApp 无法打开“md”格式的文件。NSLocalizedFailureReason= MyApp 无法打开“md”格式的文件。}
我相信我已经为降价文件正确设置了我的应用的文档类型,如下所示:
我尝试清理构建,删除派生数据并为降价文件添加“导入的 UTI”类型,但似乎没有任何效果。
奇怪的是,通过 File > Open,我可以打开 .md 文件,只是不能通过 makeDocument 以编程方式打开。
【问题讨论】:
-
makeDocument(withContentsOf:ofType:)调用documentClass(forType:),分配一个文档对象并调用init(contentsOf:ofType:)。哪个不行?
标签: swift xcode cocoa nsdocument nsdocumentcontroller