Delete All Keys from a Nsuserdefaults Dictionary iOS

Delete all keys from a NSUserDefaults dictionary iOS

If you have a look at the NSUserDefaults documentation you will see a method - (NSDictionary *) dictionaryRepresentation. Using this method on the standard user defaults, you can get a list of all keys in the user defaults. You can then use this to clear the user defaults:

- (void)resetDefaults {
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
[defs removeObjectForKey:key];
}
[defs synchronize];
}

Delete key values for NSUserDefaults in Swift

removeObjectForKey is the right way to go.

This will remove the value for the selected key. The following code sets a string value for a key in NSUserDefaults, prints it and then uses removeObjectForKey to remove and print the key value again. After removeObjectForKey the value is nil.

let prefs = NSUserDefaults.standardUserDefaults()
var keyValue = prefs.stringForKey("TESTKEY")
print("Key Value not set \(keyValue)")
let strHello = "HELLO WORLD"

prefs.setObject(strHello, forKey: "TESTKEY")
keyValue = prefs.stringForKey("TESTKEY")
print("Key Value \(keyValue)")

prefs.removeObjectForKey("TESTKEY")
keyValue = prefs.stringForKey("TESTKEY")
print("Key Value after remove \(keyValue)")

Returns:

Key Value not set nil

Key Value Optional("HELLO WORLD")

Key Value after remove nil

Update Swift 3:

let prefs = UserDefaults.standard
keyValue = prefs.string(forKey:"TESTKEY")
prefs.removeObject(forKey:"TESTKEY")

Remove NSUserDefaults for keys starting with

UserDefaults is one kind of key value pair persistent store. To solve your problem you have to follow the steps:

  • Iterate over the all keys of UserDefaults Dictionary.
  • Check each key has prefix "NavView".
  • If key has the prefix then remove the object for the key.

Swift 4:

for key in UserDefaults.standard.dictionaryRepresentation().keys {
if key.hasPrefix("NavView"){
UserDefaults.standard.removeObject(forKey: key)
}
}

Objective C :

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];

for (NSString *key in [userDef dictionaryRepresentation].allKeys) {
if ([key hasPrefix:@"start"]) {
[userDef removeObjectForKey:key];
}
}

How to auto clear NSUserDefault values in swift?

check how many keys are already stored

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

add just another key

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey1")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey2")

check how many keys are already stored again (+2)

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

now create a loop to remove your object for the keys

for key in NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys {
NSUserDefaults.standardUserDefaults().removeObjectForKey(key.description)
}

check how many keys you have again

print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)

update: Xcode 7.2.1 • Swift 2.1.1 (note NSUserDefaults doesn't work in playground anymore, so it needs to be tested in a real project)

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey1")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "justAnotherKey2")

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

for key in Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys) {
NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
}

print(Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys).count)

How to remove all UserDefaults data ? - Swift

The problem is you are printing the UserDefaults contents, right after clearing them, but you are not manually synchronizing them.

let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.synchronize()
print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count)

This should do the trick.

Now you don't normally need to call synchronize manually, as the system does periodically synch the userDefaults automatically, but if you need to push the changes immediately, then you need to force update via the synchronize call.

The documentation states this

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

Clearing NSUserDefaults

You can remove the application's persistent domain like this:

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

In Swift 3 and later:

if let bundleID = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundleID)
}

This is similar to the answer by @samvermette but is a little bit cleaner IMO.



Related Topics



Leave a reply



Submit