存储 iPad/iPhone 应用程序数据的最佳方法
我正在开发 iPad 应用程序,但不确定存储应用程序数据的最佳方式是什么。到目前为止,我一直在使用一个 .plist 来存储数百个益智游戏的字符串,效果很好,但如果我的应用程序要好用,它就必须存储数万个字符串(代表预制的字符串)谜题)。
我读过,对于大文件使用 .plist 是个坏主意,那么为 iPhone/iPad 应用程序存储大量信息(只读)的最佳方法是什么? [你能给我指点一个关于如何存储它的可靠教程吗? ]
[我不需要在任何给定时间将所有字符串加载到我的应用程序中,每轮游戏只需加载大约 50 个字符串]。
I'm developing an iPad application and I'm not sure what's the best way to store application data. So far I've been using a .plist that stores hundreds of strings for a puzzle game and that works great, but if my app is to be any good, it's going to have to store tens of thousands of strings (representing pre-made puzzles).
I've read that it's a bad idea to use .plist for large files, so what is the best way to store lots of information (read only) for an iPhone/iPad app? [Can you also point me to a solid tutorial on how to store it? ]
[I don't need to load all the strings into my application at any one given time, only around 50 per each round of the game].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的脑海中浮现出几个选项:您可以使用数据库,也可以创建存档。就我个人而言,我会使用归档方法,但 sqlite 或 CoreData 中的数据库也可以正常工作(甚至可能更快)。
要创建存档,您的类需要订阅 NSCoding 协议,实现两个方法
- (id) initWithCoder:(NSKeyedUnarchiver*)aDecoder
和- (void)encodeWithCoder:(NSKeyedArchiver* )aCoder
。这些通过调用[aCoderencodeObject: myString forKey: @"theKeyIWantToUse"];
和[self setMyString: [aDecoderdecodeObjectForKey: @"theKeyIWantToUse"]];
读取和 使用由于 Apple 对编码系统的改进,从存档中写入数据非常容易且相对较快。
同样,您也可以构建一个 CoreData 后端来管理数据库中的对象存储和检索。这抽象了数据的存储位置以及访问方式,这非常有用。希望有帮助!
You have a few options off the top of my head: You can use a database or you can create an archive. Personally, I would use the archive approach, but a database in sqlite or CoreData will work just as well (and may even be faster).
To create an archive, your classes need to subscribe to the NSCoding protocol, implementing the two methods
- (id) initWithCoder:(NSKeyedUnarchiver*)aDecoder
and- (void) encodeWithCoder:(NSKeyedArchiver*)aCoder
. These are used by calling[aCoder encodeObject: myString forKey: @"theKeyIWantToUse"];
and[self setMyString: [aDecoder decodeObjectForKey: @"theKeyIWantToUse"]];
Reading and writing data from an archive is very easy and relatively fast thanks to Apple's polish on the coding system.
Again, alternatively, you can build a CoreData backend that will manage object storage and retrieval in a database. This abstracts where data is stored and how it is accessed, which is very useful. Hope that helps!