对应用程序开发的build议iOS。 ApplicationDidEnterBackground

我很快乐,我试着做一个简单的游戏。 我在游戏中得到了一些变化。 如果applicationDidEnterBackgroundappDelegate所有其他函数保存这些variables,最佳做法是什么。

我相信是将它们存储在核心数据中,并在应用程序再次启动时加载它们。

任何有这方面经验的人?

如果你想存储和pipe理你的variables只能使用NSUserDefaults

  // Create defaults let defaults = NSUserDefaults.standardUserDefaults() // Set an int for highScoreKey in our defaults defaults.setInteger(10, forKey: "highScoreKey") // Sync/Save the defaults we've changed defaults.synchronize() // Then to retrieve our highScoreKey default we've saved // We create an int to store the value that is in our default var highScoreInt = defaults.integerForKey("highScoreKey") println(highScoreInt) // Prints 10 

我将设置并保存这些值,只要我有我需要的值,而不是在applicationDidEnterBackground虽然。

NSUserDefaults类参考

当我开始使用iOS编程时,我首先将数据存储在自定义文本文件中。 然后我将数组保存到磁盘,使用NSUserDefaults和其他工具。 最后,我决定迁移到CoreData,因为我必须保存越来越多的数据,而且我的应用程序变得太慢了。

回想一下,如果从一开始就使用CoreData,情况会好很多。 这是非常强大的,迁移到CoreData后需要很多时间。

如果你想使用CoreData,我build议从一个好的教程或一本好书开始。 我喜欢Tim Roadley的书“iOS的学习CoreData”。

一个有趣的select可能是领域 。 我还没有使用它,但它可能值得一看。

但是,这实际上取决于您的应用程序。 如果你不需要保存大量的数据,简单的方法可能就足够了(NSUserDefaults等)。

谨慎的一句话:苹果build议连续保存数据,不要等到应用程序进入后台状态。 那时,应用程序可能会随时暂停,并且您不能确定您的数据是否会及时保存。