Swift NSCOCAERRORDOMAIN Code=256无法打开“;md&x201D;格式

Swift NSCOCAERRORDOMAIN Code=256无法打开“;md&x201D;格式,swift,xcode,cocoa,nsdocument,nsdocumentcontroller,Swift,Xcode,Cocoa,Nsdocument,Nsdocumentcontroller,我正在开发一个用于编辑文件的macOS应用程序,但在尝试使用NSDocumentController.shared.makeDocument从文件URL创建新的NSDocument实例时,遇到了一个相当恼人的错误 下面是我如何调用makeDocument的一个简单示例。磁盘上存在文件test.md let url=url(fileURLWithPath:“/Users/me/Desktop/test.md” 做{ 让newDocument=尝试NSDocumentController.share

我正在开发一个用于编辑文件的macOS应用程序,但在尝试使用
NSDocumentController.shared.makeDocument
从文件URL创建新的
NSDocument
实例时,遇到了一个相当恼人的错误

下面是我如何调用
makeDocument
的一个简单示例。磁盘上存在文件
test.md

let url=url(fileURLWithPath:“/Users/me/Desktop/test.md”
做{
让newDocument=尝试NSDocumentController.shared.makeDocument(内容为:url,类型为:url.pathExtension)
打印(“已创建\(新文档)”)
}抓住{
打印(“错误:\(错误)”)
}
问题是此
try
调用失败,并到达
catch
块。我得到的错误是:

错误:由于MyApp无法以“md”格式打开文件,因此无法处理错误域=NSCOCAERRORDOMain Code=256“test.md”。由于MyApp无法以“md”格式打开文件,因此无法处理UserInfo={NSLocalizedDescription=“test.md”。NSLocalizedFailureReason=MyApp无法以“md”格式打开文件。}

我相信我已经正确设置了我的应用程序的文件类型,如下所示:

我尝试过清理构建,删除派生数据,并为标记文件添加“导入的UTI”类型,但似乎没有任何效果


奇怪的是,通过File>Open,我可以打开
.md
文件,而不是通过
makeDocument

通过XCode 10 Info.plist中的示例验证生成的Info.plist及其设置。 同时检查lsregister命令,查看您的应用程序是否已注册以处理md

lsregister(使用开关转储或读操作):

降价文件:

<dict>
    <key>CFBundleTypeName</key>
    <string>Markdown Document</string>
    <key>CFBundleTypeExtensions</key>
    <array>
        <string>md</string>
        <string>mdown</string>
        <string>markdown</string>
        <string>text</string>
    </array>
    <key>LSItemContentTypes</key>
    <array>
        <string>net.daringfireball.markdown</string>
    </array>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleTypeIconFile</key>
    <string>net-daringfireball-markdown</string>
</dict>

CbundleTypeName
降价文件
CbundleTypeExtensions
医学博士
mdown
降价
文本
lsItemContentType
net.daringfireball.markdown
CbundleTypeRole
编辑
CbundleTypeConfigile
火球减价

用XCode 10 Info.plist中的示例验证生成的Info.plist及其设置。 同时检查lsregister命令,查看您的应用程序是否已注册以处理md

lsregister(使用开关转储或读操作):

降价文件:

<dict>
    <key>CFBundleTypeName</key>
    <string>Markdown Document</string>
    <key>CFBundleTypeExtensions</key>
    <array>
        <string>md</string>
        <string>mdown</string>
        <string>markdown</string>
        <string>text</string>
    </array>
    <key>LSItemContentTypes</key>
    <array>
        <string>net.daringfireball.markdown</string>
    </array>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleTypeIconFile</key>
    <string>net-daringfireball-markdown</string>
</dict>

CbundleTypeName
降价文件
CbundleTypeExtensions
医学博士
mdown
降价
文本
lsItemContentType
net.daringfireball.markdown
CbundleTypeRole
编辑
CbundleTypeConfigile
火球减价
生成文档(包含contentsof:ofType:)
需要一个类型作为第二个参数,而不是扩展名。请查看
typeForContents(ofurl:url)
了解如何从url派生类型

参见中的图6-3

正如Marek H在他的回答中指出的那样,在info.plist中应该有一个文档类型的UTI(标识符)。

makeDocument(withContentsOf:ofType:)
需要一个类型作为第二个参数,而不是一个扩展名。看看
typeForContents(ofurl:url)
如何从url派生类型

参见中的图6-3


正如Marek H在他的回答中指出的那样,在info.plist中应该有一个文档类型的UTI(标识符)。

makeDocument(内容为:of type:)
调用
documentClass(forType:)
,分配一个文档对象并调用
init(内容为:of type:)
。哪一个不起作用?
makeDocument>(withContentsOf:ofType:)
调用
documentClass(forType:)
,分配一个文档对象并调用
init(contentsOf:ofType:)
。哪一个不起作用?他也没有正确的UTI注册“net.daringfireball.markdown”UTI不是问题,而是我没有按照上面的回答正确创建类型。他也没有正确的UTI注册“net.daringfireball.markdown”UTI不是问题,而是我没有按照上面的回答正确创建类型。