Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a user default value in NSUserDefaults?

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.

What is a user default?

The default user is a special user account in an operating system containing the default profile data for new users. For example, Microsoft Windows has a default user profile. In Windows 10, this profile is located in the directory C:\Users\, with a name of Default or something similar.


Try removeObjectForKey -- that should give you the ability to remove a preference.


Use this code

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MyKey"];

dont forget to synchronize if you want to save immediately

[[NSUserDefaults standardUserDefaults] synchronize];

NSUserDefaults Class Reference

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

Swift 5:

UserDefaults.standard.removeObject(forKey: "MyKey")
UserDefaults.standard.synchronize()

NSUserDefaults * removeUD = [NSUserDefaults standardUserDefaults];
[removeUD removeObjectForKey:@"shoping"];
[[NSUserDefaults standardUserDefaults]synchronize ];

Swift version for easy copy pasting:

var idForUserDefaults = "somestupidtext"
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.removeObjectForKey(idForUserDefaults)
userDefaults.synchronize()

or

NSUserDefaults.standardUserDefaults().removeObjectForKey("somestupidtext")
NSUserDefaults.standardUserDefaults().synchronize()