Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete key values for NSUserDefaults in Swift

How to delete data from NSUserDefaults? There is quite a few answers how to do it in Objective C, but how about Swift? So I tried this:

let defaults = NSUserDefaults.standardUserDefaults()     defaults.removeObjectForKey("myKey") 

Didn't work. Maybe what I really want to delete is not NSUserDefaults? This is how I save data:

class MySavedData: NSObject, NSCoding {     var image: String      init(name: String, image: String) {         self.image = image     }      required init(coder aDecoder: NSCoder) {         image = aDecoder.decodeObjectForKey("image") as! String     }      func encodeWithCoder(aCoder: NSCoder) {         aCoder.encodeObject(image, forKey: "image")     } }  class ViewController: <...> {     var myData = [MySavedData]() //Later myData gets modified and then function save() is called      func save() {         let savedData = NSKeyedArchiver.archivedDataWithRootObject(myData)         let defaults = NSUserDefaults.standardUserDefaults()         defaults.setObject(savedData, forKey: "myKey")     } } 

EDIT: Just to clear some things - data that is being saved is small (not even close to 100kb)

And maybe I am saving data not to NSUserDefaults (I am new to programming), so here is how I get it (load):

let defaults = NSUserDefaults.standardUserDefaults() if let savedData = defaults.objectForKey("myData") as? NSData {     myData = NSKeyedUnarchiver.unarchiveObjectWithData(savedData) as! [UserLogin]     } 
like image 271
Xernox Avatar asked Dec 17 '15 09:12

Xernox


People also ask

How do you reset NSUserDefaults?

In the simulator top menu: Simulator -> Reset Content and Settings... Show activity on this post. You can use removePersistentDomainForName method available with NSUserDefaults Class.

How do I delete data from UserDefaults?

To remove a key-value pair from the user's defaults database, you need to invoke removeObject(forKey:) on the UserDefaults instance. Let's update the previous example. The output in the console confirms that the removeObject(forKey:) method works as advertised.

What is user default in Swift?

An interface to the user's defaults database, where you store key-value pairs persistently across launches of your app.

Is NSUserDefaults secure?

Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.


1 Answers

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") 
like image 83
Peter Todd Avatar answered Sep 24 '22 06:09

Peter Todd