Local Storage in iOS Swift App | Xcode

Arun Yokesh
1 min readApr 6, 2018

--

Solution:

The simplest solution if you are just storing two strings is NSUserDefaults you could do something like below.

Keys

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

Setting

let defaults = NSUserDefaults.standardUserDefaults()

defaults.setValue("Some String Value", forKey: defaultsKeys.keyOne)
defaults.setValue("Another String Value", forKey: defaultsKeys.keyTwo)

defaults.synchronize()

Getting

let defaults = NSUserDefaults.standardUserDefaults()

if let stringOne = defaults.stringForKey(defaultsKeys.keyOne) {
println(stringOne) // Some String Value
}

if let stringTwo = defaults.stringForKey(defaultsKeys.keyTwo) {
println(stringTwo) // Another String Value
}

If you like this article. click the applause button! 👏 Follow me on Github, Twitter, Facebook.

--

--

Arun Yokesh
Arun Yokesh

No responses yet