5

I have a document based cocoa app that opens an .md file to display the markdown content in a nice format. If I change the .md file in another app like textedit, I want to reload the views in my app.

Here's what I have working so far:

import Cocoa

class Document: NSDocument {

   var fileContent = "Nothing yet :("

    override init() {    
        // Add your subclass-specific initialization here.
        super.init()
    }

    override class var autosavesInPlace: Bool {
        return false
    }

    override func makeWindowControllers() {
        // Returns the Storyboard that contains your Document window.
        let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
        let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
        self.addWindowController(windowController)
    }

    override func data(ofType typeName: String) throws -> Data {
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
    }


    override func read(from data: Data, ofType typeName: String) throws {
        fileContent = (try String(data: data, encoding: .utf8))!
    }


    // this fn is called every time textEdit changes the file content. 
    override func presentedItemDidChange() {
       // Here is the PROBLEM: 
       // HOW do I access the new file content?

    }


}

Here is the problem presentedItemDidChange() is called every time textEdit makes a change. That works great. But I can't for the life of me figure out how then to access the new file content, so I can reassign fileContent = newContent. Any thoughts?

Jamis Charles
  • 5,827
  • 8
  • 32
  • 42

1 Answers1

1

I would call for the document readFromURL:ofType:error: as described here.

Marc T.
  • 5,090
  • 1
  • 23
  • 40