How to delete a user default value in NSUserDefaults?

后端 未结 8 2038
梦谈多话
梦谈多话 2020-12-08 04:02

I.e., my app sets some standard default values at the beginning. Then those values may get overridden by the user. And when the user messes things up, I want to set those se

相关标签:
8条回答
  • 2020-12-08 04:22

    To remove a specific KEY value:

    Swift 3+

    UserDefaults.standard.removeObject(forKey: "KEY")
    

    Obj-C

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"KEY"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    If you need to reset UserDefaults/Clear All datas:

    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
    

    Swift 3:

    if let bundle = Bundle.main.bundleIdentifier {
        UserDefaults.standard.removePersistentDomain(forName: bundle)
    }
    
    0 讨论(0)
  • 2020-12-08 04:25

    Try removeObjectForKey -- that should give you the ability to remove a preference.

    0 讨论(0)
提交回复
热议问题