MacOS 应用程序无法重新打开自动保存的文件

MacOS Application can't reopen autosaved files

我正在构建一个基于 MacOS SwiftUI 文档的应用程序,我在其中定义了一个新的导出类型标识符和一个新的扩展以及所有这些。

一开始没在意Type Identifier所以取名为com.example.Dapka.dap,后来想把example去掉,改成identifier com.Dapka.dap 来自 Info.plist 和文档声明文件,但从那以后,每次我尝试重新打开自动保存的文档时,它都打不开,我在控制台中看到以下错误:

This application can't reopen autosaved com.example.dapka.dap files.

-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:] failed during state restoration. Here's the error:

Error Domain=NSCocoaErrorDomain Code=256 "The autosaved document “(null)” could not be reopened. Dapka cannot open files in the “Dapka Document” format." UserInfo={NSUnderlyingError=0x600003e5e610 {Error Domain=NSCocoaErrorDomain Code=256 "“Unsaved Dapka Document 5” could not be handled because Dapka cannot open files in the “Dapka Document” format." UserInfo={NSLocalizedDescription=“Unsaved Dapka Document 5” could not be handled because Dapka cannot open files in the “Dapka Document” format., NSLocalizedFailureReason=Dapka cannot open files in the “Dapka Document” format.}}, NSLocalizedDescription=The autosaved document “(null)” could not be reopened. Dapka cannot open files in the “Dapka Document” format., NSLocalizedFailureReason=Dapka cannot open files in the “Dapka Document” format.}

这里是Exported Type Identifier如图info.plist:

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
            <string>public.content</string>
        </array>
        <key>UTTypeDescription</key>
        <string>Dapka Document</string>
        <key>UTTypeIdentifier</key>
        <string>com.Dapka.dap</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>dap</string>
            </array>
        </dict>
    </dict>
</array>

这也是Document Types

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconSystemGenerated</key>
        <integer>1</integer>
        <key>CFBundleTypeName</key>
        <string>Dapka Document</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.Dapka.dap</string>
        </array>
        <key>NSDocumentClass</key>
        <string></string>
        <key>NSUbiquitousDocumentUserActivityType</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER).dap</string>
    </dict>
</array>

这是文档声明文件中的文档变量:

extension UTType {
    static var dapkaDocument = UTType(exportedAs: "com.Dapka.dap")
}

请注意,当我 return 标识符变回 com.example.dapka.dap 时,一切 return 都正常。我很困惑,我认为还有一些地方包含旧的 example 字。

当您更改标识符时,您实际上创建了一个新的文档类型。您使用旧标识符创建的文档不会打开,因为您更改了标识符。

如果您处于早期开发阶段,最简单的做法就是放弃旧文档。使用新标识符创建新文档。

如果您需要打开使用旧标识符的文档,请使用旧标识符创建一个 UTType。

extension UTType {
    static var oldDapkaDocument = UTType(importedAs: "com.example.Dapka.dap")
}

您的文档结构中应该有一个 readableContentsType 属性 包含 UTType 数组。将您使用旧标识符创建的 UTType 添加到数组中。

static var readableContentTypes: [UTType] { [.dapkaDocument, .oldDapkaDocument] }

现在应用程序应该能够使用旧标识符和新标识符打开文档。

我以前遇到过同样的问题。导致问题的原因是 Xcode 缓存数据,因此您可能需要清理该缓存。

1.清理构建

Select Product 从菜单中,然后单击 Clean Build Folder Shift+Command+K.

2。删除 Xcode 构建文件夹

如果上一步没有解决问题,这一步应该可以解决问题。 Derived Data 是 Xcode 存储临时构建数据和索引的文件夹,因此您可以毫无问题地删除它。

Select Xcode,然后单击 首选项 Command+,,然后 地点。现在,在派生数据下,您将看到该文件夹​​的位置,单击该位置旁边的箭头以在 Finder 中打开该文件夹,然后将其删除,然后清空垃圾箱。

或者,您可以使用终端删除文件夹:

cd /Users/<YOUR_MAC_NAME>/Library/Developer/Xcode
sudo rm -rf DerivedData