Store [String] in NSUserDefaults

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) … Read more

Working with data in iOS Apps (What to choose? NSData, CoreData, sqlite, PList, NSUserDefaults)

You can use these rules of thumb to decide what storage model will work for your app. If the data fits in memory entirely and is relatively unstructured, use plist If the data fits in memory entirely and has tree-like structure, use XML If the data does not fit in memory and has a structure … Read more

How to save local data in a Swift app?

The simplest solution for storing a few strings or common types is UserDefaults. The UserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. UserDefaults lets us store objects against a key of our choice, It’s a good idea to store these keys somewhere accessible so we … Read more