Haider Ahmed
Haider Ahmed

Reputation: 169

User Defaults in swift 3 not working

i have to save the password locally on my device by using the UserDefaults but it is not saving any value this is my code :

// this is for the bool value but not saving i don't know
var reg = true

let userDefault = UserDefaults.standard
userDefault.set(reg, forKey: "reg")
let getBoolValue = userDefault.bool(forKey: "reg")


// this also for the string value but also not working
let userDefaul = UserDefaults.standard
userDefault.set("123456", forKey: "password")
let getPassword = userDefault.value(forKey: "password") as? String

Upvotes: 2

Views: 1173

Answers (1)

Annie Gupta
Annie Gupta

Reputation: 2822

May be you are trying it at playground. Try it in a real project.

But would like to point out a typo here:

let userDefaul = UserDefaults.standard
userDefault.set("123456", forKey: "password")
let getPassword = userDefault.value(forKey: "password") as? String

You are using userDefault here again, instead of userDefaul

Also the earlier code would also work but just that you were trying to use userDefaul so thought of pointing out. Cheers!

Upvotes: 3

Related Questions