如何以编程方式打开NSDocument macOS应用程序中的文档?

问题描述:

我是新来的macOS编程,我创建了一个NSDocument应用程序项目来学习这种架构。如何以编程方式打开NSDocument macOS应用程序中的文档?

所有工作正常,我可以创建一个文档,保存它,并使用标准的UI控件从Finder中打开一个。

我试图保存并以编程方式打开文档。我实施了这两项行动。保存工作正常,但阅读不起作用。编辑:我的意思是文档窗口不显示。

如果有人能告诉我我做错了什么,我将不胜感激。

// this seems to work because the document is created and I can open it with drag and drop over the app 
@IBAction func saveButtonPressed(_ sender: Any) { 
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first // or .applicationSupportDirectory 

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL) 

    if (try? document.write(to: docURL!, ofType: "com.myCompany.docuExperiments")) != nil { 
    } else { 
     print("An error occured") 
    } 
} 

// the document window is not shown 
@IBAction func loadButtonPressed(_ sender: Any) { 
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first // or .applicationSupportDirectory 

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL) 

    if (try? document.read(from: docURL!, ofType: "com.myCompany.docuExperiments")) != nil { 
    } else { 
     print("An error occured") 
    } 
} 
+0

你能比“阅读不工作”更具体吗?究竟发生了什么? –

+0

'loadButtonPressed'应该创建一个新的文档对象还是重新加载一个现有的文档对象? – Willeke

+0

嗨抢,文档窗口不显示。我会更新这个问题,谢谢。 – Cue

NSDocument管理从URL显示文档。它是NSDocumentController,应该用于启动文件保存的开始(尽管如您所发现的,没有任何东西从停止NSDocument写入文档的副本)。

docs

NSDocumentController创建和管理文件

的应用程序的NSDocumentController对象管理在应用程序的文件。 在MVC设计模式中,NSDocumentController对象是一个 高级控制器。它具有以下主要职责:

  • 创建空文件响应的新建项目在文件菜单
  • 会在文件菜单响应与数据初始化从文件到 打开项目文件
  • 跟踪和管理这些文档
  • 把手 文档相关的菜单项,如打开最近

(我的重点)

,如果你想使用自己的程序,你需要继承NSDocumentController,把你打开例行程序在那里。但它已经有了所有的逻辑,所以你真正需要做的就是将你的按钮挂到文件 - >打开的目标,这将是func beginOpenPanel(completionHandler: @escaping ([URL]?) -> Void)默认NSDocumentController。所以没有必要这样做。刚刚阅读NSDocumentController文档(上面链接)

+0

不,不要继承'NSDocumentController'来打开文档。 – Willeke

+0

你读过我的答案了吗? OP的问题是关于文件对话框,我指出*没有必要这么做*。但是,如果他想按照书面实施他的惯例,那就是做这件事的地方。但他不需要。 – Grimxn

+0

我读“所以你需要子类NSDocumentController”。 – Willeke

这似乎工作。

保存文档:

@IBAction func saveButtonPressed(_ sender: Any) { 
    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first 

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL) 

    if (try? document.write(to: docURL!, ofType: "com.myCompany.docuExperiments")) != nil { 
    } else { 
     print("An error occured") 
    } 
} 

打开文档:

@IBAction func loadButtonPressed(_ sender: Any) { 
    let documentController = NSDocumentController.shared() 

    let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first 

    let docURL = URL(string:"MyFile.docuExperiments", relativeTo:directoryURL) 

    documentController.openDocument(withContentsOf: docURL!, display: true) { 
     // completionHandler (NSDocument?, Bool, Error?) 
     (document, documentWasAlreadyOpen, error) in 
     if error != nil 
     { print("An error occured") 
     } else { 
      if documentWasAlreadyOpen 
      { 
       print("documentWasAlreadyOpen: true") 
      } else { 
       print("documentWasAlreadyOpen: false") 
      } 
     } 
    } 

} 

(感谢Willeke的提示)。