• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

How to save data, settings & preferences with NSUserDefaults

Home ios How to save data, settings & preferences with NSUserDefaults

How to save data, settings & preferences with NSUserDefaults

Jan 5, 2016 | Posted by Andrew | ios, swift, tutorial, xcode |

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:

Screen Shot 2016-01-04 at 2.33.16 pm

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

Screen Shot 2016-01-04 at 3.36.19 pm

DownloadSourceCode

Tags: dataloadnsuserdefaultssave
1
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

How to access the Camera and Photos in Swift.

Oct 3, 2016

What you will learn How to access the user’s camera[...]

Tutorial – Pass data between View Controllers using a Segue

Aug 2, 2015

In the last tutorial we looked at navigating view controllers[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next