Jinesh Patel
Jinesh Patel

Reputation: 11

removing value from userdefault is not working :: Issue with userdefault

To store value in UserDefaults, i am doing UserDefaults.standard.setValue(true, forKey: "kIsUserLoggedIn")

To remove value from UserDefaults, i am doing UserDefaults.standard.removeObject(forKey: "kIsUserLoggedIn")

removeObject is not working.

I have tried below ways:

  1. UserDefaults.standard.setValue(nil, forKey: "kIsUserLoggedIn")
  2. Tried by executing in 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.

NOTE: I am having machine with M1 chip, can this matter for this issue?

Upvotes: 1

Views: 1574

Answers (2)

Vishal_VE
Vishal_VE

Reputation: 2127

To storing and removing boolean value inside Userdefault you can create your own extension shown below:-

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()
}

}

Upvotes: 0

Tarun Tyagi
Tarun Tyagi

Reputation: 10092

You should clarify in your question how you are accessing the value for "kIsUserLoggedIn" after removal of this value.

// 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.

// 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.

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. 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.


As @vadian mentioned above, you shouldn't need to remove the value for this key from defaults entirely. You can set this to false and then work with the Bool directly in all cases instead of the way you are trying currently.


Here's a simple example of how to use a Bool value stored in UserDefaults.

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
}

Upvotes: 2

Related Questions