How to save data, settings & preferences with NSUserDefaults
One of the very first questions you have after learning how to make iPhone apps is how to save and load data such as settings & preferences in your app. Well this post will cover how to achieve this using NSUserDefaults.
What you will learn
- What NSUserDefaults is
- What types of data you should save in NSUserDefaults
- How to save & load data from NSUserDefaults
What is NSUserDefaults?
NSUserDefaults are used to save small amounts of data in a app in a .plist file. It can save basic data types such as Strings, Integers. Large amounts of data should be saved in a different area such a sqlite or Core Data.
Setup for this Tutorial
Create a new XCode project, and layout the storyboard as follows:
Connect these objects up to your ViewController.swift, the data label as an outlet named lblData and the buttons as an action named btnSave and btnLoad respectively. Your code should now look something like the following:
import UIKit class ViewController: UIViewController { @IBOutlet var lblData: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func btnSave(sender: AnyObject) { } @IBAction func btnLoad(sender: AnyObject) { } }
Saving data using NSUserDefaults
To save data to NSUserDefaults add code as follows. Under the outlet lblData add:
@IBOutlet var lblData: UILabel! let userDefaults = UserDefaults.standard
The let userDefaults is basically a shorthand way of allowing us to reference the NSUserDefaults.standardUserDefaults() in the code which is used to save & retrieve data from NSUserDefaults
Secondly to save data to NSUserDefaults add the following to btnSave so it looks like:
@IBAction func btnSave(sender: AnyObject) { let data = "Hello" userDefaults.set(data, forKey: "keyHello") }
The data we are saving in this example is just simply the string “Hello”. This stored this in the constant data. To finally save that we need to set an object in NSUserDefaults. The data in this is the information we are saving to NSUserDefaults and forKey is a reference to the data.
Think of the forKey as a filename, it is a unique identifier used to reference and load saved data. Now when we need to load the data later on we will need to use this “keyHello” to retrieve it.
Loading data using NSUserDefaults
To load the “Hello” string we saved add the following to the btnLoad:
let data: String? = userDefaults.object(forKey: "keyHello") as! String? if(data != nil) { lblData.text = data } else { lblData.text = "Nothing Saved" }
This creates a data constant as an optional, it then attempts to load the item referenced from the key “keyHello” into data. As it is an optional if there has been no item saved for key it won’t load anything which will result in it being nil. If there has been an item saved it will return that item and save in data in our case it is the string “Hello”.
It then checks if there was anything saved for the key “keyHello”, if so it will set the label’s text to the data. If nothing has been saved it will simply set the label text to “Nothing Saved”.