我可以使用 NSDocument 写入资源分支吗?
我想将一些附加信息与文档一起存储,但我无法使用捆绑包或包,也无法将其存储在文档本身中。
该应用程序是一个文本编辑器,我希望它能够将代码折叠和书签位置与文档一起存储,但显然这不能直接嵌入到代码中,而且我不想用丑陋的注释来更改代码。
我可以使用 NSDocument 在文档的资源分支中存储信息吗?如果是这样,我该怎么做?我应该直接写入
还是有可用的 API?
I'd like to store some additional information along with a document, but I can't use bundles or packages, and I cannot store it inside the document itself.
The application is a text editor, and I'd like it to store code folding and bookmark locations with the document, but obviously this cannot be embedded into the code directly, and I don't want to alter the code with ugly comments.
Can I use NSDocument to store information in the resource fork of a document? If so, how can I do this? Should I directly write to <filename>/..namedfork/rsrc
or is there an API available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,不要使用资源分支。它实际上已被弃用。相反,请使用扩展属性。它们可以通过
setxattr
和getxattr
。扩展属性用在很多地方……例如,在最新的 OS X 中,资源分支本身就是作为一种特殊类型的扩展属性实现的。例如,Cocoa 文本系统自动向文件添加扩展属性来指定编码。
我认为
NSFileManager
和NSFileWrapper
自 Snow Leopard 以来就支持扩展属性,但我找不到任何文档 :p 不过,您始终可以使用 BSD 级别的函数。First, don't use the resource fork. It's virtually deprecated. Instead, use extended attributes. They can be set programmatically at the BSD level via
setxattr
andgetxattr
. Extended attributes are used in many places... for example, in the latest OS X, the resource fork itself is implemented as a special type of extended attributes.For example, the Cocoa text system automatically adds an extended attribute to a file to specify the encoding.
I thought
NSFileManager
andNSFileWrapper
supported extended attributes since Snow Leopard, but I can't find any documentation :p You can always use the BSD level functions, though.如果将文件复制到另一台计算机,状态是否需要随文件一起移动?如果没有,您可能会做得比模拟 Bare Bones 使用 BBEdit 处理文档状态的方式更糟糕。它们将所有文档的状态存储在 ~/Library/Preferences/com.barebones.bbedit.PreferenceData/Document State.plist 中。
资源分支文档在这里 。但它包含大量不使用资源分支的建议。
Does the state need to move with the file if it's copied to another computer? If not, you could do a lot worse than emulating the way Bare Bones handles document state with BBEdit. They store state for all documents in ~/Library/Preferences/com.barebones.bbedit.PreferenceData/Document State.plist.
The resource fork documentation is here. But it contains plenty of suggestions to not use the resource fork.
我的网站上有一个用于读取和编写资源分支的课程,但我一直没有时间将其转移到我的 GitHub 存储库,因为正如 Yuji 指出的那样,它们不再真正被使用。
我想说别名文件和网络互联网位置文件是它们唯一使用的地方,但我在 Mac OS X v10.7 (Lion),甚至不再使用它们;它们仍可用于自定义图标。我没有专门对此进行测试。我必须看看这对 10.7 上的 NDalias 类有何影响。
ndresourcefork
I have a class on my web site for reading and writing resource forks, which I have never got around to moving to my GitHub repository because, as Yuji points out, they are not really used any more.
I was going to say alias files and web Internet location file are the only places they are used, but I used and tested it on Mac OS X v10.7 (Lion), and they are not even used there any more; they may still be used for custom icons. I didn't test for that exclusively. I will have to see how that affect my NDAlias class on 10.7.
ndresourcefork