NSTreeController如何保存到文件
您好,我正在使用 NSTreeController 来控制 NSOutlineView。该应用程序将书签从文件加载到应用程序。如 ADC 中的 SourceView 示例所示:
http://developer.apple。 com/mac/library/samplecode/SourceView/index.html
我的问题是,一旦用户进行更改,如何将书签保存到文件中。我应该在应用程序内部维护数组/树并在退出之前保存还是有更简单的方法?
Hi I am using an NSTreeController to control an NSOutlineView. This application loads bookmarks from file to application. As in the SourceView example in ADC:
http://developer.apple.com/mac/library/samplecode/SourceView/index.html
My questions is how do I save the bookmark to file once user makes the changes. Should I maintain the array/tree internally in my application and save before quitting or is there any easier methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要反转 MyWindowController.m 的 populateOutline 方法中发生的操作。此方法将 plist 读入一个字典,从该字典中读取一个值,然后使用它来构建树。从该方法开始,按照代码查看它是如何构建树的。它使用 BaseNode 和 ChildNode 类将数据模型构建为树(我不知道为什么他们不只使用 NSTreeNode >)。您想要反转该过程,最终得到一个 NSDictionary。然后,您可以使用 writeToFile:atomically: 将字典保存回磁盘。
这可以变得像您想要的那样复杂。例如,当前代码在单独的线程中加载字典文件,因此您也可以在单独的线程中保存。或者,您可能希望在每次编辑后再次保存在单独的线程中。
You want to reverse the action taking place in the populateOutline method of MyWindowController.m. This method is reading the plist into one dictionary, reading a value from that dictionary, and using it to build the tree. Start with that method and follow the code to see how it is building the tree. It's using the BaseNode and ChildNode classes to build up the data model as a tree (I'm not sure why they didn't just use NSTreeNode). You want to reverse that procedure, ending up with an NSDictionary. You can then use writeToFile:atomically: to save the dictionary back to disk.
This can get as complex as you'd like to make it. For instance, the current code loads the dictionary file in a separate thread, so you could save in a separate thread, too. Or, you might want to save after every edit, again in a separate thread.