Cocoa:使用 NSDocument 从另一个应用程序更改文件后如何重新读取文件内容?

Jam*_*les 5 macos cocoa nsdocument swift

我有一个基于文档的可可应用程序,它打开一个 .md 文件,以良好的格式显示降价内容。如果我在另一个应用程序(例如 textedit)中更改 .md 文件,我想在我的应用程序中重新加载视图。

这是我迄今为止所做的工作:

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?

    }


}
Run Code Online (Sandbox Code Playgroud)

presentedItemDidChange()这是每次 textEdit 进行更改时都会调用的问题。效果很好。但我一生都无法弄清楚如何访问新的file content,所以我可以重新分配fileContent = newContent。有什么想法吗?