How to save local data in a Swift app? How to save local data in a Swift app? ios ios

How to save local data in a Swift app?


The simplest solution if you are just storing two strings is NSUserDefaults, in Swift 3 this class has been renamed to just UserDefaults.

It's best to store your keys somewhere globally so that you can reuse them elsewhere in your code.

struct defaultsKeys {    static let keyOne = "firstStringKey"    static let keyTwo = "secondStringKey"}

Swift 3.0, 4.0 & 5.0

// Settinglet defaults = UserDefaults.standarddefaults.set("Some String Value", forKey: defaultsKeys.keyOne)defaults.set("Another String Value", forKey: defaultsKeys.keyTwo)// Gettinglet defaults = UserDefaults.standardif let stringOne = defaults.string(forKey: defaultsKeys.keyOne) {    print(stringOne) // Some String Value}if let stringTwo = defaults.string(forKey: defaultsKeys.keyTwo) {    print(stringTwo) // Another String Value}

Swift 2.0

// Settinglet defaults = NSUserDefaults.standardUserDefaults()defaults.setObject("Some String Value", forKey: defaultsKeys.keyOne)defaults.setObject("Another String Value", forKey: defaultsKeys.keyTwo)// Gettinglet defaults = NSUserDefaults.standardUserDefaults()if let stringOne = defaults.stringForKey(defaultsKeys.keyOne) {    print(stringOne) // Some String Value}if let stringTwo = defaults.stringForKey(defaultsKeys.keyTwo) {    print(stringTwo) // Another String Value}

For anything more serious than minor config, flags or base strings you should use some sort of persistent store - A popular option at the moment is Realm but you can also use SQLite or Apples very own CoreData.


They Say Use NSUserDefaults

When I was implementing long term (after app close) data storage for the first time, everything I read online pointed me towards NSUserDefaults. However, I wanted to store a dictionary and, although possible, it was proving to be a pain. I spent hours trying to get type-errors to go away.

NSUserDefaults is Also Limited in Function

Further reading revealed how the read/write of NSUserDefaults really forces the app to read/write everything or nothing, all at once, so it isn't efficient. Then I learned that retrieving an array isn't straight forward. I realized that if you're storing more than a few strings or booleans, NSUserDefaults really isn't ideal.

It's also not scalable. If you're learning how to code, learn the scalable way. Only use NSUserDefaults for storing simple strings or booleans related to preferences. Store arrays and other data using Core Data, it's not as hard as they say. Just start small.

Update: Also, if you add Apple Watch support, there's another potential consideration. Your app's NSUserDefaults is now automatically sent to the Watch Extension.

Using Core Data

So I ignored the warnings about Core Data being a more difficult solution and started reading. Within three hours I had it working. I had my table array being saved in Core Data and reloading the data upon opening the app back up! The tutorial code was easy enough to adapt and I was able to have it store both title and detail arrays with only a little extra experimenting.

So for anyone reading this post who's struggling with NSUserDefault type issues or whose need is more than storing strings, consider spending an hour or two playing with core data.

Here's the tutorial I read:

http://www.raywenderlich.com/85578/first-core-data-app-using-swift

If you didn't check "Core Data"

If you didn't check "Core Data"when you created your app, you can add it after and it only takes five minutes:

http://craig24.com/2014/12/how-to-add-core-data-to-an-existing-swift-project-in-xcode/

http://blog.zeityer.com/post/119012600864/adding-core-data-to-an-existing-swift-project

How to Delete from Core Data Lists

Delete Data from Coredata Swift


Okey so thanks to @bploat and the link to http://www.codingexplorer.com/nsuserdefaults-a-swift-introduction/

I've found that the answer is quite simple for some basic string storage.

let defaults = NSUserDefaults.standardUserDefaults()// Storedefaults.setObject("theGreatestName", forKey: "username")// Receiveif let name = defaults.stringForKey("username"){    print(name)    // Will output "theGreatestName"}

I've summarized it here http://ridewing.se/blog/save-local-data-in-swift/