nice
nice

Reputation: 31

How do I use NSUserDefaults in Swift

I'm new to Swift and I was wondering how I use NSUserDefaults. when save the data it looks to save but when I call it it doesn't load the data

For example:

defaults.setObject(nice, forKey: "nicer")

and when I call it, it will not load any of the data in it.

Upvotes: 1

Views: 287

Answers (2)

jbiser361
jbiser361

Reputation: 987

welcome to stack overflow! you want to sync them. for example:

UserDefaults.standard.synchronize()

Upvotes: 1

Mohammed Zaki
Mohammed Zaki

Reputation: 131

Here is how I usually use UserDefaults in Swift, some steps are optional or versatile depend on your application.

// creates an easy reference to user defaults it is optional 
    let container = UserDefaults.standard
//You can use some variable like that or enum 
    let valueToStore : String = "Persist some value"
//how I set information to the key
    container.set("New persisted data", forKey: valueToStore)
//how I retrieve information from the defaults for a key
    container.value(forKey: valueToStore)

Upvotes: 4

Related Questions