1.Writting Preferences
defaults.setObject(self.firstNameTextField.text, forKey: "firstName")
defaults.synchronize()
2.Reading Preferences
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if let firstNameIsNotNill = defaults.objectForKey("firstName") as? String {
self.firstNameTextField.text = defaults.objectForKey("firstName") as String
}
Using NSUserDefaults:
One of the fastest mechanism to store small bulk of data consist of using NSUserDefault
It can save and read data from the following type:
NSData
NSString
NSNumber
NSDate
NSArray
NSDictionary
To Write / Read data you can use
//Write
var defUser = NSUserDefaults.standardUserDefaults()
defUser.setObject(data, forKey: stringKey)
//Read
var defUser = NSUserDefaults.standardUserDefaults()
var value = defUser.stringForKey(forKey: stringKey)
Notice It’s only working on a sandbox environment related to App , so if you are testing it on Playground It may not work!