簡體   English   中英

Cocoa:如何在使用NSDocument從另一個應用程序更改文件后重新讀取文件內容?

[英]Cocoa: How to re-read file content after file has been changed from another app using NSDocument?

我有一個基於文檔的cocoa應用程序,它打開一個.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?

    }


}

這是每次textEdit進行更改時調用presentItemDidChange presentedItemDidChange() 的問題 這很好用。 但我不能為我的生活弄清楚如何訪問新的file content ,所以我可以重新分配fileContent = newContent 有什么想法嗎?

我會呼吁文檔readFromURL:ofType:error:描述在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM