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()
}