简体   繁体   English

我应该如何存储Cocoa应用数据?

[英]How should I store Cocoa app data?

I'm just getting started on making a board game using Cocoa/Objective-C. 我刚刚开始使用Cocoa / Objective-C制作棋盘游戏。 My plan is to first get it functional as a Mac OS X app, and then port it to iOS. 我的计划是首先让它作为Mac OS X应用程序运行,然后将其移植到iOS。

I saw something in the documentation for the Core Data API that made it sound like it was the recommended way to store data persistently, and made reference to the fact that iOS apps should be able to quit and be restored in exactly the same state. 我在Core Data API的文档中看到了一些内容,它使得它听起来像是持久存储数据的推荐方法,并且引用了iOS应用程序应该能够退出并在完全相同的状态下恢复的事实。 I got the initial impression that I should plan to use Core Data for any variable that has to do with the game's state to support this. 我得到的初步印象是,我应该计划将Core Data用于任何与游戏状态有关的变量来支持这一点。

But as I'm learning more it seems like my initial impression isn't correct. 但随着我的学习越来越多,我的初步印象似乎并不正确。 Core Data seems more like something intended to provide similar features as embedded SQL, and is more complicated than is required just for storing the game state persistently on disk. 核心数据似乎更像是用于提供与嵌入式SQL类似的功能,并且比将游戏状态持久存储在磁盘上所需的更复杂。 Is there a simpler way to support fast app restoring in iOS apps, or is Core Data the way to go? 有没有更简单的方法来支持iOS应用程序中的快速应用程序还原,或者Core Data是否可行?

Core Data lets you store data as objects, so if your game state can be described with native data types, Core Data just might work for you. Core Data允许您将数据存储为对象,因此,如果您的游戏状态可以使用本机数据类型进行描述,则Core Data可能适合您。 You'd essentially be manipulating object states. 你基本上是在操纵对象状态。

If you're looking for something a little more lightweight, look into the NSUserDefaults API (which is the same thing that the Settings App on iOS uses). 如果您正在寻找更轻量级的东西,请查看NSUserDefaults API(这与iOS上的Settings App使用的相同)。

Alternatively, you could come up with your own format and use that, serializing your own data to disk. 或者,您可以提出自己的格式并使用它,将您自己的数据序列化到磁盘。

I'd start with NSUserDefaults. 我从NSUserDefaults开始。

Core Data is fantastic for storing lots of data of different types, including custom objects. Core Data非常适合存储大量不同类型的数据,包括自定义对象。

However, if you're talking about storing things like high scores for a game or other simple int , float , BOOL , NSString , NSArray data, then for iOS NSUserDefaults is a quick and easy way to go. 但是,如果您正在讨论存储游戏的高分或其他简单的intfloatBOOLNSStringNSArray数据等内容,那么对于iOS NSUserDefaults来说,这是一种快速简便的方法。

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

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