0

Not sure how to close this question, but a comment resolved my problem

When I try to change view controllers in my code I get this error

@RMaddy No, this is not a duplicate, My error is due to defaults, but the error is taking place in a transition of view controllers. So before you rush to conclusions, please read the question.

(Further Clarification) No, the class I am trying to instantiate from the storyboard does have the correct class (id) and the correct identifier, so that is not the problem

2016-02-24 10:04:25.271 Shapes Game[3834:27900] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Shapes_Game.ScoreViewController 0x7ff263e87150> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key highScore.'
*** First throw call stack:
(

My code worked fine before I added this to my code (All the NSUserDefaults and updating the high score)

I'm not sure why my code is causing an error, but any help would be appreciated.

I Apologize in advance if my code is too difficult to read. If you need clarification on what I need help in, just comments below. (I just want to know what I can do to fix this)

func updateHighScore() {
    let score: Int? = NSUserDefaults.standardUserDefaults().valueForKey("highScore") as? Int
    if let highScore = score {
        if highScore < totalScore {
            highScoreLabel.text = String(totalScore)
        }
        else {
            highScoreLabel.text = highScore.formatted()
        }
    }
    else {
        highScoreLabel.text = totalScore.formatted()
    }
}
func gameOver(){
     loadHighScore() 
     if let scoreVC =     storyboard!.instantiateViewControllerWithIdentifier("gameOver") as?    ScoreViewController {
                scoreVC.score = self.totalScore
                presentViewController(scoreVC, animated: true,      completion: nil)

      }
//setting high score
        let score: Int? = NSUserDefaults.standardUserDefaults().valueForKey("highScore") as? Int
        if let highScore = score {
            if highScore < totalScore {
                NSUserDefaults.standardUserDefaults().setObject(totalScore, forKey: "highScore")
            }
            else {
                //nothing
            }
        }
        else {
            NSUserDefaults.standardUserDefaults().setObject(totalScore, forKey: "highScore")
        }
        NSUserDefaults.standardUserDefaults().synchronize()
 }
Ronak Shah
  • 936
  • 9
  • 17
  • And yes, I have looked at every single other stack overflow question addressing this, I cannot find what my error is. – Ronak Shah Feb 24 '16 at 18:17
  • Did you read the highest voted answer to the duplicate question? You have a problem with your ScoreViewController storyboard. – rmaddy Feb 24 '16 at 18:25
  • You said you have looked at every single Stack Overflow question, but if that were true I think you'd have the answer. When you get this error it's almost certainly because you deleted an IBOutlet in code but didn't unhook it in Interface Builder. – Ben Kane Feb 24 '16 at 18:26
  • Regarding your statement that it's about User Defaults, no it's not. You just happen to be using the same key name for userDefaults. Change to a different key and you'll still get the same error message. – Ben Kane Feb 24 '16 at 18:29
  • thanks @BenKane you helped. rmaddy that question did not help, sorry – Ronak Shah Feb 24 '16 at 18:34
  • /close (idk how to close questions) – Ronak Shah Feb 24 '16 at 18:34
  • No problem. It's already marked as a duplicate, it doesn't need to be closed. The question it's linked to does answer your question. Maybe you didn't read the answers fully. The highest upvoted answer for that question ("You may have a bad connection in your xib"), is what you ran into. – Ben Kane Feb 24 '16 at 18:40

0 Answers0