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 = … Read more

How can I reset the NSUserDefaults data in the iPhone simulator?

You want NSUserDefaults removePersistentDomainForName. This will remove all user defaults for the application: NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; For more information on the NSUserDefaults class see the Apple docs. Alternately, if all you are concerned with is data in the iOS Simulator, you can do that via iOS Simulator > Reset … Read more

Saving UIColor to and loading from NSUserDefaults

One way of doing it might be to archive it (like with NSColor, though I haven’t tested this): NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color]; [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@”myColor”]; And to get it back: NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@”myColor”]; UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

How to save NSMutablearray in NSUserDefaults

Note: NSUserDefaults will always return an immutable version of the object you pass in. To store the information: // Get the standardUserDefaults object, store your UITableView data array against a key, synchronize the defaults NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:arrayOfImage forKey:@”tableViewDataImage”]; [userDefaults setObject:arrayOfText forKey:@”tableViewDataText”]; [userDefaults synchronize]; To retrieve the information: NSUserDefaults *userDefaults = [NSUserDefaults … Read more

What is the use of -[NSUserDefaults registerDefaults:]?

The difference is that the first code-snippet you register defaults that will be used when the user has not made any changes to the “property”. So if you want to provide let’s say a “property” with the key name ‘Welcome message’, you could instead of having the property returning nil insert a default message ‘Welcome … Read more

How to remove all UserDefaults data ? – Swift [duplicate]

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 … Read more

Save custom objects into NSUserDefaults [duplicate]

Actually, you will need to archive the custom object into NSData then save it to user defaults and retrieve it from user defaults and unarchive it again. You can archive it like this let teams = [Team(id: 1, name: “team1”, shortname: “t1”), Team(id: 2, name: “team2”, shortname: “t2”)] var userDefaults = UserDefaults.standard let encodedData: Data … Read more

Storing authentication tokens on iOS – NSUserDefaults vs Keychain?

I would highly recommend you use the keychain – it’s exactly what Facebook do for storing their session tokens. NSUserDefaults is not secure or encrypted – it can be easily opened and read, both on device and when synced to a Mac. So whilst user defaults is a good place for things like preferences and … Read more

Where is a Mac Application’s NSUserDefaults Data Stored?

They can be found in more than one place: ~/Library/Preferences/com.example.myapp.plist ~/Library/SyncedPreferences/com.example.myapp.plist and if sandboxed ~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist ~/Library/Containers/com.example.myapp/Data/Library/SyncedPreferences/com.example.myapp.plist

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)