2

I'm building a MacOS SwiftUI Document-Based App where I defined a new Exported Type Identifier with a new extension and all of that.

In the beginning, I didn't care about the Type Identifier so I named it com.example.Dapka.dap, but later on, I tried to get rid of the example word, changing the identifier to com.Dapka.dap from Info.plist and Document declaration file, but since then, everytime I try to reopen an autosaved document, it won't open, and I see the following error in the console:

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.}

Here is Exported Type Identifier as shown in 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>

And this is Document Types as well:

<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>

And this is the document variable in the Document declaration file:

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

Notice that, when I return the identifier back to com.example.dapka.dap, everything returns normal. I'm very confused, I think there is still some place that contains the old example word.

Adam M.
  • 85
  • 10

2 Answers2

2

I have been through the same problem before. What causes the problem was Xcode cashed data, so you might need to clean that cache.

1. Clean the build

Select Product from the menu, then click Clean Build Folder Shift+Command+K.

2. Delete Xcode Build Folder

If the previous step didn't fix it, this one should do the trick. Derived Data is the folder where Xcode stores temporary build data and indexes, so you can delete it without problems.

Select Xcode from the menu, then click Preferences Command+,, then Locations. Now, under Derived Data, you will see the location of that folder, click on the arrow beside the location to open the folder in Finder, and delete it, then empty the trash.

Alternatively, you can delete the folder using Terminal:

cd /Users/<YOUR_MAC_NAME>/Library/Developer/Xcode
sudo rm -rf DerivedData
Sami Almalki
  • 588
  • 2
  • 17
1

When you changed the identifier, you essentially created a new document type. Documents you created with the old identifier are not going to open because you changed the identifier.

If you are early in development, the easiest thing to do is abandon the old documents. Create new documents using the new identifier.

If you need to open documents that use the old identifier, create a UTType with the old identifier.

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

There should be a readableContentsType property in your document struct that contains an array of UTTypes. Add the UTType you created with the old identifier to the array.

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

Now the app should be able to open documents with the old and the new identifier.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66