简体   繁体   English

从 userdefault 中删除值不起作用 :: userdefault 问题

[英]removing value from userdefault is not working :: Issue with userdefault

To store value in UserDefaults, i am doing UserDefaults.standard.setValue(true, forKey: "kIsUserLoggedIn")为了在 UserDefaults 中存储值,我正在执行UserDefaults.standard.setValue(true, forKey: "kIsUserLoggedIn")

To remove value from UserDefaults, i am doing UserDefaults.standard.removeObject(forKey: "kIsUserLoggedIn")要从 UserDefaults 中删除值,我正在执行UserDefaults.standard.removeObject(forKey: "kIsUserLoggedIn")

removeObject is not working. removeObject 不起作用。

I have tried below ways:我尝试过以下方法:

  1. UserDefaults.standard.setValue(nil, forKey: "kIsUserLoggedIn")
  2. Tried by executing in DispatchQueue.main.async通过在DispatchQueue.main.async执行尝试

To generate this issue, -> Delete derived data -> Fresh Install app, after successful login, store value and remove.要生成此问题,-> 删除派生数据-> 全新安装应用程序,成功登录后,存储值并删除。 -> Install the app again, it will work fine -> Uninstall and install again. -> 再次安装该应用程序,它会正常工作 -> 卸载并再次安装。 -> Login the app and logout -> Reinstall from Xcode and check at AppDelegate, value is not removed even after logging out. -> 登录应用程序并注销 -> 从 Xcode 重新安装并检查 AppDelegate,即使注销后也不会删除值。

NOTE: I am having machine with M1 chip, can this matter for this issue?注意:我的机器带有 M1 芯片,这对这个问题有影响吗?

You should clarify in your question how you are accessing the value for "kIsUserLoggedIn" after removal of this value.您应该在您的问题中澄清删除此值后如何访问"kIsUserLoggedIn"的值。

// Value is removed from storage
UserDefaults.standard.removeObject(forKey: "kIsUserLoggedIn")

// prints `nil` as there's nothing stored for this key anymore
print(UserDefaults.standard.object(forKey: "kIsUserLoggedIn"))

All fine up to now, in case you are using UserDefaults.bool(forKey:) api, it will rightfully print false .到目前为止一切正常,如果您使用UserDefaults.bool(forKey:) api,它会正确地打印false

// prints `false`
print(UserDefaults.standard.bool(forKey: "kIsUserLoggedIn"))

The Boolean value associated with the specified key.与指定键关联的布尔值。 If the specified key doesn't exist, this method returns false .如果指定的键不存在,则此方法返回false

This method automatically coerces certain ”truthy” values—such as the strings "true", "YES", and "1", and the numbers 1 and 1.0—to the Boolean value true.此方法会自动将某些“真实”值(例如字符串“true”、“YES”和“1”以及数字 1 和 1.0)强制为布尔值 true。 The same is true for certain ”falsy” values—such as the strings "false", "NO", and "0", and the numbers 0 and 0.0—which are automatically coerced to the Boolean value false.对于某些“假”值(例如字符串“false”、“NO”和“0”以及数字 0 和 0.0),情况也是如此——它们会被自动强制转换为布尔值 false。


As @vadian mentioned above, you shouldn't need to remove the value for this key from defaults entirely.正如@vadian 上面提到的,您不需要完全从默认值中删除此键的值。 You can set this to false and then work with the Bool directly in all cases instead of the way you are trying currently.您可以将其设置为false ,然后在所有情况下直接使用Bool ,而不是您当前尝试的方式。


Here's a simple example of how to use a Bool value stored in UserDefaults .下面是一个简单的示例,说明如何使用存储在UserDefaultsBool值。

extension UserDefaults {
    var isUserLoggedIn: Bool {
        get { self.bool(forKey: #function) }
        set { self.setValue(newValue, forKey: #function) }
    }
}

// Usage
UserDefaults.standard.isUserLoggedIn = true // upon log in
UserDefaults.standard.isUserLoggedIn = false // upon log out

// when app launches
if UserDefaults.standard.isUserLoggedIn {
   // user is logged in
}

To storing and removing boolean value inside Userdefault you can create your own extension shown below:-要在 Userdefault 中存储和删除布尔值,您可以创建自己的扩展,如下所示:-

extension UserDefaults {扩展用户默认值 {

//MARK:- Store bool value in Userdefault
public class func bool(forKey: String) -> Bool {
 
     let boolValue = UserDefaults.standard.bool(forKey: forKey)
     return boolValue
    
}

/**
 removes object from NSUserDefault stored for a given key
 
 - parameter defaultName: key for object
 */
class func removeObjectForKey(_ defaultName: String) {
    UserDefaults.standard.removeObject(forKey: defaultName)
    UserDefaults.standard.synchronize()
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM