简体   繁体   English

使用NSUserDefaults保存游戏设置/状态的优点?

[英]Advantages of using NSUserDefaults to save game settings/state?

Is there any benefit in going with NSUserDefaults to save game state/settings over creating a binary file in plain C? 使用NSUserDefaults来保存游戏状态/设置而不是在普通C中创建二进制文件有什么好处吗?

Specifically for an iOS game. 专门针对iOS游戏。

The biggest advantage with NSUserDefaults is that you don't have to worry about the actual writing to "disk" — -synchronize is automatically called at "appropriate times", which includes app background (if multitasking is supported) or termination (if multitasking is not supported, or you set UIApplicationExitsOnSuspend or whatever it's called). NSUserDefaults的最大优点是您不必担心实际写入“磁盘” - 在“适当的时间”自动调用-synchronize ,其中包括应用程序后台(如果支持多任务)或终止(如果多任务处理是不支持,或者您设置UIApplicationExitsOnSuspend或其他任何名称)。 This means you can update NSUserDefaults much more frequently without as much overhead. 这意味着您可以更频繁地更新NSUserDefaults而无需太多开销。

The disadvantage is that I'm pretty sure the whole thing is loaded into memory and a write involves writing the whole file (I think it even writes to a temporary file and then does a rename), so it's not suitable for storing large, infrequently-changing blobs (eg photos). 缺点是我很确定整个东西被加载到内存中并且写入涉及写入整个文件(我认为它甚至写入临时文件然后进行重命名),因此它不适合存储大量的,不经常的 - 改变blob(例如照片)。

Unless your saved games are huge, I'd do something reasinably simple with NSUserDefaults, mostly because it avoids you having to keep track of a bunch of files. 除非你保存的游戏很大,否则我会用NSUserDefaults做一些非常简单的事情,主要是因为它避免了你必须跟踪一堆文件。

EDIT: A potentially major drawback with NSUserDefaults is that doesn't save when the app crashes, so you might still want to explicitly -synchronize occasionally, depending on how crashy your app is. 编辑:有一个NSUserDefaults的潜在的主要缺点是不能当保存应用程序崩溃,所以你可能想要明确地-synchronize偶尔,这取决于你的应用程序如何crashy是。 This is particularly noticable for developers, since pressing "stop" in Xcode (or unplugging the phone while debugging) is effectively a crash! 这对开发人员来说尤其明显,因为在Xcode中按“停止”(或在调试时拔掉手机)实际上是一次崩溃!

NSUserDefaults is the appropriate location for settings, but probably not save games. NSUserDefaults是适合设置的位置,但可能无法保存游戏。

The advantage is you get in-memory caching and several decades of stability behind it. 优点是你可以获得内存缓存和几十年的稳定性。 The downside is you are fairly limited in what you can put in a user defaults file (objects must be plist -friendly, ie strings, numbers, data, dates, dictionaries, and arrays only.) As well, saving game state in NSUserDefaults would make it more difficult to handle multiple save games. 缺点是你可以放在用户默认文件中的相当有限(对象必须是plist友好的,即字符串,数字,数据,日期,字典和数组。)同样,在NSUserDefaults保存游戏状态会使处理多个保存游戏变得更加困难。

I would recommend you build your logic classes such that they can be serialized/deserialized to/from an arbitrary byte stream (easiest way to do that is to implement NSCoding on your various game state classes.) Then you can just dump files to disk to arbitrary locations (eg a "quicksave" copy in NSUserDefaults and a list of full savegames in [yourapp]/Documents/ . The task of encoding/decoding is not actually strongly associated with the task of storage/retrieval, as surprising as that might be. 我建议你构建你的逻辑类,以便可以对任意字节流进行序列化/反序列化(最简单的方法是在你的各种游戏状态类上实现NSCoding 。)然后你可以只将文件转储到磁盘任意位置(在例如“quicksave”复制NSUserDefaults ,充满游戏存档的列表中的[yourapp]/Documents/ 。编码的任务/解码实际上并不强烈存储/检索任务相关联,如令人惊讶,因为这可能是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM