在 iOS 应用程序上保存大文件
我想知道哪种方法是为 iOS 应用程序存储大文件的最佳方法(Apple 指南或常识建议的方法)。
大我指的是例如游戏的保存(可以是 500kb)因此考虑 plist 是不可行的。
我应该使用普通的 Cocoa 文件管理 API 来保存它吗?标准 stdio
(fopen
、fwrite
等)怎么样?
I was wondering which is the best way (the one suggested by Apple guidelines or by common sense) to store big files for an iOS application..
With big I mean for example the save of a game (which can be, let's say 500kb) so that thinking about plists is unfeasible.
Should I just save it using normal Cocoa file management API? What about standard stdio
(fopen
, fwrite
, whatever)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的首要任务应该是总体用户体验,包括:
根据我的经验,大文件操作往往相对慢,但CPU相对快,因此如果您有大量文本要保存,请使用zlib来压缩它的保存效果很好(文本压缩率很高,而且 zlib 相当快)。
Your top priority should be overall user experience, including:
In my experience, big file operations tend to be relatively slow, but the cpu is relatively fast, so if you have a huge chunk of text to save, using zlib to compress it for saving works well (text compression rates are high, and zlib is pretty fast).
有很多选择,当然最好的选择取决于您的要求。如果你愿意的话,你当然可以使用 C 标准 I/O 的东西。如果您将所有游戏数据存储在一个大缓冲区中,则 NSData 的读取/写入文件方法很容易使用。如果您有实现 NSCoding 的对象集合,则可以使用 NSKeyedArchiver/Unarchiver。
以下是使用 NSFileManager 查找应用程序的文档目录的一种方法:
There are plenty of options, and of course the best one depends on your requirements. You can certainly use the C standard I/O stuff if you want. If you've got all your game data in one big buffer, NSData's read from/write to file methods are easy to use. If you've got a collection of objects that implement NSCoding, you can use NSKeyedArchiver/Unarchiver.
Here's one way to use NSFileManager to locate your app's Documents directory: