NSBundle,可以用来保存项目文档吗?
我正在构建一个可用于创建“项目”的 Mac OS X 应用程序。当用户保存项目时,他们将保存许多资源:图像文件、文本文件、sqlite 文件等。
我可以在文档中为每个项目创建一个文件夹,在该文件夹中我可以放置所有项目资源,然后只需包括用于打开项目的单个项目文件。
我读过有关 NSBundle 的内容,我想使用它。但我只在应用程序包的上下文中读到过它们。可以这样使用 NSBundle 吗?用户只能看到一个文件,并且可以将其移动到任何他们喜欢的地方。
使用 NSBundle 做我正在尝试的事情有意义吗?或者还有其他方法可以做到这一点吗?
(我对 MacOS X 编程相当陌生)
更新
我相信 iPhoto 使用此方法来存储“iPhoto Library”,这就是我想对我的应用程序执行的操作,NSBundle 就是我想要的应该调查吗?
I'm building a Mac OS X application that can be used to create 'projects'. When a user saves a project, they will be saving many resources: image files, text files, sqlite files etc.
I can either create a folder in Documents for each project, and within that folder I can place all the project assets, and just include a single project file that is used to open the project.
I've read about NSBundle which I'd like to use. But I've only read about them in the context of application bundles. Is it possible to use NSBundle in this way? Where the user only sees a single file, and can move it wherever they like.
Does it make sense to do what I'm trying, using NSBundle? Or is there another way to do this?
(I'm fairly new to MacOS X programming)
UPDATE
I believe iPhoto uses this method to store the "iPhoto Library", this is what I'd like to do with my application, is NSBundle what I should be looking into?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可能的,并且过去已经在很多应用程序中使用过。该方法在 本文档。在应用的
Info.plist
中明确声明后,Finder 会将生成的捆绑包显示为一个文件。但是,我不建议您这样做。苹果自己的Keynote过去使用的是捆绑方式,但现在不再使用它了。同样,OmniGraffle(这是 OS X 上历史悠久的图表应用程序)使用捆绑包来保存项目,但它也停止这样做了。
原因是,尽管 Finder 将其显示为文件,但非 Apple 电子邮件软件或任何浏览器等仍将捆绑包视为目录。例如,如果用户想要将捆绑格式的文档附加到她/他在浏览器内的 Gmail 中撰写的电子邮件中,那就会很混乱。这让很多人感到困惑。
所以,这是可能的,但我不推荐它。一种解决方法是使用压缩包作为用户可见的文件,并在用户将其打开到临时目录时将其解压缩。然后你可以使用 NSBundle 和/或 NSFileWrapper api 来访问其中的文件。
Yes, it is possible and was used in quite a few apps in the past. The method is described in this document. Once it's clearly declared in your app's
Info.plist
, Finder will show the resulting bundle as one file.However, I can't recommend you to do that. Apple's own Keynote was using the bundle approach in the past, but it no longer uses it any more. Similarly, OmniGraffle (which is a diagramming app on OS X with a long history) used bundles to save projects, but it stopped doing that, too.
The reason is that the bundle is still seen as a directory by the non-Apple email software, or any browser, etc, although the Finder shows it as a file. It would be a mess if a user wants to attach your document in the bundle format to an email s/he is composing in Gmail inside a browser, say. That confused a lot of people.
So, it's possible, but I don't recommend it. One way out is to use the zipped bundle as the user-visible file, and to unzip it when the user opens it into a temporary directory. Then you can use
NSBundle
and/orNSFileWrapper
apis to access files inside it.Apple 的 File Wrapper 示例似乎就是您想要的:
http://developer.apple.com/library/mac/ #samplecode/PersistentDocumentFileWrappers/Introduction/Intro.html
它还演示了如何在包中保存核心数据持久存储。如果您只想存储资源,可以忽略该部分。
Apple's File Wrapper sample seems to be what you want:
http://developer.apple.com/library/mac/#samplecode/PersistentDocumentFileWrappers/Introduction/Intro.html
It also demonstrates how to save a Core Data persistent store in the bundle. You can leave that part out if you just want to store resources.
NSBundle 类代表应用程序包,可用于访问应用程序内的资源,但您希望使用它来获取应用程序数据,而不是用户数据。
对于您提到的每种资源,都有一种保存此类型的方法,例如,对于图像文件,您可以使用 NSData 将图像数据保存到磁盘,对于文本文件,您可以使用方法
writeToFile:atomically:encoding:error:
。您可能非常想看看核心数据(http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html),一个非常好的管理用户数据模型的框架,看看这是否满足您的需求。
The NSBundle class represents the application bundle, and can be used to access resources within the applicationm but you would you it for application data, not user data.
For each of the resources you mention, there's a way of saving this type, for example, for image files, you could use NSData to save the image data to disk, and for text files you could use the method
writeToFile:atomically:encoding:error:
.You may very well want to take a look at Core Data (http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html), a very good framework for managing the user's data model, to see if this would fit your needs.