【问题标题】:Change data types stored in NSUserDefault with app update [closed]使用应用程序更新更改存储在 NSUserDefault 中的数据类型 [关闭]
【发布时间】:2014-12-15 09:04:03
【问题描述】:

我使用 NSUserDefaults 保存了一些保存在我的应用中的数据,例如一些具有 NSNumber 属性的对象。

@interface STAccountInfo : NSObject

@property(nonatomic,strong)NSNumber* is_new_device_verification_enabled;
@property(nonatomic,strong)NSNumber* is_new_device_verification_enabled_by_enterprise;
@property(nonatomic,strong)NSNumber* is_collab_related_function_restricted;

self.is_new_device_verification_enabled = [decoder decodeNumberForKey:@"is_new_device_verification_enabled"];;

但我的新版本需要将一些 NSNumber 属性更新为 bool 类型。

@interface STAccountInfo : NSObject

@property(nonatomic,assign)BOOL is_new_device_verification_enabled;
@property(nonatomic,assign)BOOL is_new_device_verification_enabled_by_enterprise;
@property(nonatomic,assign)BOOL is_collab_related_function_restricted;
如果我尝试解码存储的对象,应用程序将由于错误的数据类型而引发异常。我应该在解码器部分做什么?

这是当前的解码代码:

self.is_new_device_verification_enabled = [decoder decodeBoolForKey:@"is_new_device_verification_enabled"];

【问题讨论】:

  • 你能把代码贴在这里

标签: ios objective-c decode


【解决方案1】:

我建议您为新用户默认值使用新的键名,然后在启动时:

  1. 检查旧密钥是否存在 - 如果它们不存在,则照常进行。
  2. 如果确实存在,请将旧键值迁移到新键(例如,将“1”更改为“是”)
  3. 删除旧键的值

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2016-09-18
    相关资源
    最近更新 更多