Reputation: 6321
I have the following block of code
let defaults = NSUserDefaults.standardUserDefaults()
let username = self.emailText.text
let password = self.passwordText.text
defaults.setValue(username, forKey: defaultsKeys.userKey)
defaults.setValue(password, forKey: defaultsKeys.passwordKey)
var u = defaults.stringForKey(defaultsKeys.userKey)
var p = defaults.stringForKey(defaultsKeys.passwordKey)
username and password are correct when setting. However, when I pull them back u and p have the same value, and it's the password value.
I declare defaultKeys at the top of the page this way.
struct defaultsKeys {
static let userKey = ""
static let passwordKey = ""
}
I'm assuming there's a simple syntax error that I'm not seeing, because this seems like it should work.
Upvotes: 2
Views: 75
Reputation: 416
struct defaultsKeys
{
static let userKey = "userKey"
static let passwordKey = "passwordKey"
}
Upvotes: 1
Reputation: 708
Set different key names:
struct defaultsKeys {
static let userKey = "userKey"
static let passwordKey = "passwordKey"
}
Upvotes: 3
Reputation: 527
You have the same key for both, just set a different key for each one:
struct defaultsKeys {
static let userKey = "USER_KEY"
static let passwordKey = "PWD_KEY"
}
Upvotes: 3