Yesterday my code worked perfectly fine. However I changed the bundle identifier and know the high score won’t save. I don’t know what happened. The high score is calculated in totalTime. Notice in the picture the label with high score 0.0.
import UIKit
class resultsViewController: UIViewController {
@IBOutlet var SCOREXX: UILabel!
public var LebelText: String?
@IBOutlet var lebelTEXTED: UILabel!
@IBOutlet var HIGHSCOREXX: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
timeCalculation()
loadState()
// Do any additional setup after loading the view.
}
func saveScore(score: Double) {
// Instantiate user defaults
let userDefaults:UserDefaults = UserDefaults.standard
// Set your score
userDefaults.set(score, forKey: "highScore")
}
func loadState() {
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
HIGHSCOREXX.text = "High Score: \(score)"
}
func timeCalculation(){
guard let unwrapedText = self.LebelText else {
return
}
if let myInt = Double(unwrapedText)
{
var totalTime = myInt
self.SCOREXX.text = "You won"+"\n"+"Reaction time :" + String(totalTime) + " Seconds"
guard let highScore = UserDefaults.standard.value(forKey: "highScore") as? Double
else{return}
if totalTime>highScore{
saveScore(score: totalTime)
}}}}