iPhone-从旧的应用程序首选项升级 [英] iPhone - upgrading from old app preferences

查看:85
本文介绍了iPhone-从旧的应用程序首选项升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用默认设置的2个字符串构建1.0应用程序,请使用registerDefaults .
在2.0版本中,我决定删除第一个旧字符串,然后将第二个旧字符串(更改其键字符串)与新的第三个字符串一起移动到数组中.
我该如何处理以及如何处理通过版本对内容所做的更改.

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应该是

StringKey    someValue  
DateKey      10/10/2010

1.1偏好设置

StringKey    someValue  
DateKey      2010/10/10

2.0 Prefs应该是

Array  
    Item0 is    DateKey        10/10/2010  
    Item1 is    BadString      BadBadValue

推荐答案

如果您在应用程序的1.0版中使用了registerDefaults:,则非常简单.当您停止注册那些旧值并且用户未更改它们时,它们将从NSUserDefaults中消失.

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.

因此,向NSUserDefaults询问应转换的所有对象.如果存在,请将其转换为新格式,将其保存在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.

类似的事情应该起作用

// check if you can get the old object. if it's there it was changed by the user
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"OldKey1"]) {
    // old key is present
    id oldObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"OldKey1"];

    id newObject = ... // convert the old object to the new object
    [[NSUserDefaults standardUserDefaults] setObject:newObject forKey:@"NewKey1"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"OldKey1"];
}
else {
    // old key not there, do nothing
}

// register your new defaults...

如果您未使用过registerDefaults:,则现在遇到了问题.因为您不知道该对象是否由用户更改,或者该对象仅仅是您的默认对象.而且您不能仅仅因为该值与默认值相同就认为该值仍处于默认状态.

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.

但是在这种情况下该怎么办?我可能会将值重置为默认值,并显示一个UIAlert来告诉用户检查首选项,因为我做错了:-)

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 :-)

这篇关于iPhone-从旧的应用程序首选项升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆