Jhorra
Jhorra

Reputation: 6321

Storing values locally using NSUserDefaults

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

Answers (3)

SaiPavanParanam
SaiPavanParanam

Reputation: 416

struct defaultsKeys 
{
static let userKey = "userKey"
static let passwordKey = "passwordKey"
}

Upvotes: 1

André Muniz
André Muniz

Reputation: 708

Set different key names:

struct defaultsKeys {
    static let userKey = "userKey"
    static let passwordKey = "passwordKey"
}

Upvotes: 3

mtaweel
mtaweel

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

Related Questions