xcode 存储 iPad/iPhone 应用程序数据的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3491631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:38:20  来源:igfitidea点击:

Best Ways to Store iPad/iPhone Application Data

iosiphonexcodestoring-data

提问by

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).

我正在开发 iPad 应用程序,但不确定存储应用程序数据的最佳方式是什么。到目前为止,我一直在使用一个 .plist 来存储数百个用于拼图游戏的字符串并且效果很好,但是如果我的应用程序要好用,它将不得不存储数万个字符串(代表预制拼图)。

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? ]

我已经读到将 .plist 用于大文件是一个坏主意,那么为 iPhone/iPad 应用程序存储大量信息(只读)的最佳方法是什么?[你能不能给我一个关于如何存储它的可靠教程?]

[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].

[我不需要在任何给定时间将所有字符串加载到我的应用程序中,每轮游戏只需大约 50 个]。

回答by Grimless

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).

我脑子里有几个选项:您可以使用数据库,也可以创建存档。就我个人而言,我会使用归档方法,但 SQLite 或 CoreData 中的数据库也能正常工作(甚至可能更快)。

To create an archive, your classes need to subscribe to the NSCoding protocol, implementing the two methods - (id) initWithCoder:(NSKeyedUnarchiver*)aDecoderand - (void) encodeWithCoder:(NSKeyedArchiver*)aCoder. These are used by calling [aCoder encodeObject: myString forKey: @"theKeyIWantToUse"];and [self setMyString: [aDecoder decodeObjectForKey: @"theKeyIWantToUse"]];

要创建存档,您的类需要订阅 NSCoding 协议,实现两个方法- (id) initWithCoder:(NSKeyedUnarchiver*)aDecoder- (void) encodeWithCoder:(NSKeyedArchiver*)aCoder. 这些由调用[aCoder encodeObject: myString forKey: @"theKeyIWantToUse"];和使用[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.

由于 Apple 对编码系统的完善,从存档中读取和写入数据非常容易且相对较快。

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!

同样,或者,您可以构建一个 CoreData 后端来管理数据库中的对象存储和检索。这抽象了数据的存储位置和访问方式,这非常有用。希望有帮助!

回答by vakio