如何在新版本中迁移 NSUserDefaults?

发布于 2024-08-22 06:30:23 字数 360 浏览 6 评论 0原文

我有一个 iPhone 应用程序,它使用 NSUserDefault standardUserDefaults 存储一些设置。

当我向应用程序添加新功能时,我需要添加新的默认设置,因此我必须迁移/升级 NSUserDefaults。目前,我只是存储版本号并在应用程序启动时检查它。它很快就变得非常混乱,因为我必须添加大量 if 语句。我不能假设用户只是从以前的版本升级,但可能甚至是之前的几个版本

我喜欢 CoreData 处理迁移表更改的方式,但我想提供 2.2.1 SDK 兼容性,当然 CoreData 与 NSUserDefaults 不同。

有什么建议或最佳实践吗?

I have an iPhone app that stores some settings using NSUserDefault standardUserDefaults.

When I add new features to the app I need to add new default settings and so I have to migrate/upgrade the NSUserDefaults. For now, I just store the version number and check this when the app is launched. It has quickly become very messy as I have to add lots of if statements. I cannot assume that the user is just upgrading from the previous version but perhaps even a couple of versions before.

I like the way that CoreData seems to handle migrating table changes but I want to provide 2.2.1 SDK compatibility and of course CoreData is not the same thing as NSUserDefaults.

Any suggestions or best practices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-08-29 06:30:23

嗯……我不会以这种方式“升级”用户默认值,而是像往常一样查询 NSUserDefaults,只有在未设置默认值的情况下(在这种情况下 objectForKey 将返回 nil),才使用默认值(可能是写回用户默认以节省下次时间)。由于每次读取“新”默认值(即 1.0 中不存在的默认值)时都必须执行此操作,因此我建议执行此操作的包装类/文件,这样代码仅编写一次每个“新”默认值。

另外,虽然问题/问题不同,这个答案在您的情况下也同样有效案件。

Hmm… I wouldn't "upgrade" the user defaults in this way, but instead query NSUserDefaults as usual, only if the default isn't set (objectForKey will return nil in that case), use a default value (which may then be written back to the user defaults to save time the next time). Since you'll have to do this every time a "new" default (i.e. one that didn't exist in 1.0) is read, I advise doing a wrapper class/file that does this, this way the code is written only once for each "new" default.

Also, while the question/problem is different, this answer works just as well in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文