B.Saravana Kumar B.Saravana Kumar - 4 years ago 1138
Swift Question

How to auto clear NSUserDefault values in swift?

How can I auto clear the

nsuserdefault
values in swift? I have already tried this but it doesn't work for me...

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];

Answer Source

check how many keys are already stored

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

println(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

println(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array.count)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download