Store [String] in NSUserDefaults

Thomas picture Thomas · Aug 21, 2014 · Viewed 44.9k times · Source

I want to save a Swift Style String Array into NSUserDefaults, but acutally the "if" statement in the code says that returnValue is always nil.

Later in the code (iOS 8) I want to use "food += ["spaghetti"] to add new entries.

var food : [String] {
    get {
        var returnValue : [String]? = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String]
        if returnValue == nil      //Check for first run of app
        {
            returnValue = ["muesli", "banana"]; //Default value
        }
        return returnValue!
    }
    set (newValue) {
        NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
}

Answer

Imanou Petit picture Imanou Petit · Aug 21, 2014

The following code should help you resolve your problem:

import UIKit

class ViewController: UIViewController {

    var food: [String] {
        get {
            if let returnValue = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] {
                return returnValue
            } else {
                return ["muesli", "banana"] //Default value
            }
        }
        set {
            NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print(food) // prints: ["muesli", "banana"] (at first launch)

        food = ["cake"]
        print(food) // prints: ["cake"]

        food += ["spaghetti"]
        print(food) // prints: ["cake", "spaghetti"]

        food = []
        print(food) // prints: []

        NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "food")
        print(food) // prints: ["muesli", "banana"]
    }

}

However, with the previous code, if you set food = [], you will have a problem as food won't return ["muesli", "banana"]. In order to avoid this, you may prefer the following code:

import UIKit

class ViewController: UIViewController {

    var food: [String] {
        get {
            if let returnValue = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] {
                return returnValue == [] ? ["muesli", "banana"] : returnValue
            } else {
                return ["muesli", "banana"] //Default value
            }
        }
        set {
            NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print(food) // prints: ["muesli", "banana"] (at first launch)

        food = ["cake"]
        print(food) // prints: ["cake"]

        food += ["spaghetti"]
        print(food) // prints: ["cake", "spaghetti"]

        food = []
        print(food) // prints: ["muesli", "banana"]

        NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "food")
        print(food) // prints: ["muesli", "banana"]
    }

}