Question

I would like to save local data in my app, and need to use it sometime. The data could be a response from server.

The application is a swift-build application.

Answer

In Swift 3, we can use UserDefaults to handle local data. Below is the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Setting

let defaults = UserDefaults.standard
defaults.set("One", forKey: defaultsKeys.keyOne)
defaults.set("Two", forKey: defaultsKeys.keyTwo)

// Getting

let defaults = UserDefaults.standard
if let stringOne = defaults.string(forKey: defaultsKeys.keyOne) {
print(stringOne) // One
}
if let stringTwo = defaults.string(forKey: defaultsKeys.keyTwo) {
print(stringTwo) // Two
}

Reference


This is the end of post