0
votes

I'm writing a Cocoa Document based app.

I've gotten it to the point where it can save a document using

    override func data(ofType typeName: String) throws -> Data {
    // End editing
    let jsonEncoder = JSONEncoder()
    let jsonData = try jsonEncoder.encode(self)
    let jsonString = String(data: jsonData, encoding: .utf8)
    Swift.print(jsonString)
    return jsonData
}

I've got a corresponding read method

    override func read(from data: Data, ofType typeName: String) throws {
    let jsonDecoder = JSONDecoder()
    let rep = try jsonDecoder.decode(Document.self, from: data) as! Document
    videoURL = rep.videoURL
    timestamps = rep.timestamps
}

But it never seems to get called. If I start the app and use the open menu item, I'd expect the read method to be called after I select the file to open, but instead it just opens a window with a new document.

I'm stumped. I've got a hunch it might have something to do with the info.plist, or the entitlements file, but I can't understand what.

I'd appreciate any help or ideas.

1
How did you start your project? Did you use the Cocoa App template from Xcode and selected "Create Document-Based Application"? If not, create another project with that option and compare the settings. Maybe you missed something. Also, you can paste your code into the new project. - Dirk

1 Answers

0
votes

You're on the right track. For the Open command to function it has to correlate the type of the file its opening with the document class in your app. You do that in the "Document Types" section in the "Info" tab of your application target settings.

Select your app target and select the Info tab. Find the "Document Types" section. In it, you'll need to define your document type, assign it an identifier, specify its extension and which icon it gets, and—most importantly—the class name of your custom NSDocument subclass.