iPhone - 从旧的应用程序首选项升级
如果我构建一个默认有 2 个字符串的 1.0 应用程序,使用 registerDefaults。
在 2.0 版本中,我决定删除第一个旧字符串,并将第二个旧字符串(更改其密钥字符串)与新的第三个字符串一起移动到数组中。
我该如何处理这个问题以及如何处理可能通过版本对内容进行的更改。
1.0 首选项应为
StringKey someValue
DateKey 10/10/2010
1.1 首选项应为
StringKey someValue
DateKey 2010/10/10
2.0 首选项应为
Array
Item0 is DateKey 10/10/2010
Item1 is BadString BadBadValue
If I build a 1.0 app with 2 strings in the defaults, using registerDefaults.
And in a 2.0 version, I decide to remove the first old string, and move the second one (changing its key string) with a new third one into an array.
How may I deal with this and how to deal with the changes that could have been made to the content through versions.
1.0 Prefs should be
StringKey someValue
DateKey 10/10/2010
1.1 Prefs should be
StringKey someValue
DateKey 2010/10/10
2.0 Prefs should be
Array
Item0 is DateKey 10/10/2010
Item1 is BadString BadBadValue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在应用的 1.0 版中使用过
registerDefaults:
,那么这很简单。当您停止注册这些旧值并且它们没有被用户更改时,它们就会从 NSUserDefaults 中消失。因此,请向 NSUserDefaults 询问所有应转换的对象。如果它们存在,请将它们转换为新格式,将它们保存在 NSUserDefaults 中并删除旧值。
类似这样的东西应该可以工作
如果您还没有使用
registerDefaults:
,那么您现在遇到了问题。因为您不知道该对象是否被用户更改,或者该对象是否只是您的默认对象。并且您不能仅仅因为该值与默认值具有相同的值就假设该值仍处于默认状态。但这种情况该怎么办呢?我可能会将值重置为默认值并显示 UIAlert 告诉用户检查首选项,因为我犯了一个错误:-)
If you've used
registerDefaults:
in version 1.0 of your app it's easy. When you stop to register those old values and they were not changed by the user they disappear from NSUserDefaults.So ask NSUserDefaults for all objects that should be converted. If they exist convert them to the new format, save them in the NSUserDefaults and remove the old value.
Something like this should work
If you haven't used
registerDefaults:
you have a problem now. Because you don't know if the object was changed by the user or if the object is simply your default. And you can't assume that the value is still in default state just because it has the same value as the default.But what to do in this case? I would probably reset the value to default and show a UIAlert that tells the user to check the preferences because I did a mistake :-)