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] }
In the simulator top menu: Simulator -> Reset Content and Settings... Show activity on this post. You can use removePersistentDomainForName method available with NSUserDefaults Class.
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.
An interface to the user's defaults database, where you store key-value pairs persistently across launches of your app.
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.
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
let prefs = UserDefaults.standard keyValue = prefs.string(forKey:"TESTKEY") prefs.removeObject(forKey:"TESTKEY")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With