Hi ,
To use userDefault in swift we can use below code to
1- Save value in userDefaults for a key
2- Fetch value for a key
3- Remove value for a key
4- Remove all value
Here is the code -
//Save a value in NSUserDefaults in swift
let userDefault = NSUserDefaults().standardUserDefaults()
userDefault .setValue(accessToken, forKey: KEY_TO_SAVE_VALUE)
//remove a value from NSUserDefault in swift
let userDefault = NSUserDefaults.standardUserDefaults()
userDefault.removeObjectForKey(KEY_TO_REMOVE_VALUE)
//remove all the values in NSUserDefaults in swift
let appdomain = NSBundle.mainBundle().bundleIdentifier
let userPref = NSUserDefaults.standardUserDefaults()
userPref.removePersistentDomainForName(appdomain!)
//print all the values from NSUserDefaults in swift
print("all the user default values here:\n(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())")
0 Comment(s)