objective-c 在 iPhone 上保存数据的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307313/
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
Best way to save data on the iPhone
提问by Chris Jefferson
I am writing an iPhone application, and need to save the state of my application (5K or so).
我正在编写一个 iPhone 应用程序,需要保存我的应用程序的状态(5K 左右)。
My main worry is data persisting across upgrades. Some of the applications I use clearly got this wrong, and I would prefer not to!
我主要担心的是数据在升级过程中持续存在。我使用的一些应用程序显然是错误的,我宁愿不要!
回答by Ben Gottlieb
To save state, NSUserDefaults is the way to go. I believe most, if not all, instances of application data being deleted after an upgrade are due to issues on the AppStore. They may be related to data-format changes, but if you use just NSUserDefaults and standard plist-storable objects (NSString, NSDictionary, NSNumber, NSArray, NSNumber, and primitives), you should be safe.
要保存状态, NSUserDefaults 是要走的路。我相信大多数(如果不是全部)应用程序数据在升级后被删除的实例都是由于 AppStore 上的问题。它们可能与数据格式更改有关,但如果您只使用 NSUserDefaults 和标准 plist 可存储对象(NSString、NSDictionary、NSNumber、NSArray、NSNumber 和原语),那么您应该是安全的。
回答by Sirens
There's a simply way to do exactly this To save:
有一种简单的方法可以做到这一点 保存:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToFindText"];
To load:
装载:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *textToLoad = [prefs stringForKey:@"keyToFindText"];
The important thing is that you do not store very large values in NSUserDefaults. There's a chance of getting in trouble with apple review if you do.
重要的是您不要在NSUserDefaults. 如果你这样做,就有可能在苹果方面遇到麻烦。
Generally, user defaults is used for storing short keys and preferences such as the users volume level, wether they want to use game center, or send crash logs.
通常,用户默认值用于存储快捷键和首选项,例如用户音量级别、是否要使用游戏中心或发送崩溃日志。
回答by August
It really depends on what you're doing and your needs.
这真的取决于你在做什么和你的需求。
If it's a small amount of data NSUserDefaults is a great way to go. You can also save a dictionary or an array to your apps Documents folder, then load that file back in on launch.
如果是少量数据 NSUserDefaults 是一个很好的方法。您还可以将字典或数组保存到您的应用程序 Documents 文件夹,然后在启动时重新加载该文件。
SQLite is nice, but unless you've got a lot of data that you'll be querying, it's a little over the top for just saving state.
SQLite 很好,但除非您有大量要查询的数据,否则仅保存状态就有点过头了。
回答by pixel
I use sqlite to store all application data, and preferences. To make sure that updates do not wipe the data, make sure the sqlite file is stored in the Documents directory of the application, which is not overwritten by upgrades. Some of the example code ("SQLite Books" I think) Apple provides has code to handle this.
我使用 sqlite 来存储所有应用程序数据和首选项。为确保更新不会擦除数据,请确保 sqlite 文件存储在应用程序的 Documents 目录中,该目录不会被升级覆盖。苹果提供的一些示例代码(我认为是“SQLite Books”)有代码来处理这个问题。
回答by Iggy
for small amounts of data NSUserDefaults is a perfect solution, if you need ad hoc querying of data sqlite is the way to go. Those two solutions are perfect for storing data on the device, if you need more flexibility and capability you could consider remote data i.e web services and a server database.
对于少量数据 NSUserDefaults 是一个完美的解决方案,如果您需要临时查询数据 sqlite 是要走的路。这两种解决方案非常适合在设备上存储数据,如果您需要更多的灵活性和功能,您可以考虑远程数据,即 Web 服务和服务器数据库。
回答by Mark7777G
Hereis a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. @Ben Gottlieb's solution is on target, I just thought this question would add an additional resource. Hope this helps!
这是一个讨论的链接,该讨论提供了一个很好的示例,说明了如何访问您的数据以及在某些情况下哪种类型的存储方案会更有利。@Ben Gottlieb 的解决方案是有针对性的,我只是认为这个问题会增加额外的资源。希望这可以帮助!
回答by JamesSugrue
The most common way to save state (data, application information etc) is to use Sqlite3. I'm not sure how an update would wipe it, but it's possible that the update process could overwrite the datafile.
保存状态(数据、应用程序信息等)的最常用方法是使用Sqlite3。我不确定更新会如何擦除它,但更新过程可能会覆盖数据文件。
The other option would be to use UserPrefs (NSUserDefaults). Again, I'm not sure the max size for that, or even if it would be suitable for the data you need to persist.
另一种选择是使用 UserPrefs (NSUserDefaults)。同样,我不确定它的最大大小,或者即使它适合您需要保留的数据。

