Haden Yo Haden Yo -5 years ago 205
Swift Question

NSuserDefault valueForKey error swift two

I am trying to save a highscore in a simple game. I am new to swift 2 and not sure what I am doing wrong. i am getting the error that "Value of type 'int' has no member for 'valueForKey'. Here is my code as of my last unsuccesful attempt.

var highScoreDefault = NSUserDefaults.standardUserDefaults()

if(highScoreDefault.valueForKey("highScore") != nil){
highScore = highScore.valueForKey("highScore") as NSInteger! //error here

score += 1
if (score > highScore){
highScore = score
var highScoreDefault = NSUserDefaults.standardUserDefaults()
highScoreDefault.setValue(highScore, forKey: "highScore")
highscoreLbl.text = "\(highScore)"

Answer Source

Its very simple to set value in NSUserDefault

NSUserDefaults.standardUserDefaults().setInteger(yourScore, forKey: "highScore")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download