0

Instance variables:

var Score: Int64 = 0

 var highscore: Int64 = 0
      func saveHighScore(high:Int) {
        NSUserDefaults.standardUserDefaults().setInteger(high, forKey:        "highscore")

    }
  func highScore() -> Int {
   NSUserDefaults.standardUserDefaults().integerForKey("highscore")
   } func resetHighScore() {NSUserDefaults.standardUserDefaults().removeObjectForKey("highscore")
    }

///METHOD. ALL OF THIS IS WORKING FINE. HIGH SCORE SHOWS AND UPDATES WHILE IN THE GAME.

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
 @IBAction func Ans1(sender: AnyObject) {
    if(CorrectAnswer) == "1" {

        // Calculate the number of points
        Score += 10
        scoreLabel.text = "Score:\(Score)"
        if (Score > highscore) {
            highscore = Score
            highScoreLabel.text = NSString(format: "High Score : %i",    highscore) as String
        }

Do I need to add anything here? For data to save and reload on relaunch of the application?

maxshuty
  • 9,708
  • 13
  • 64
  • 77
  • Just to confirm, you want to removeObjectForKey in NSUserDefaults whenever the view disappears or the app enters the background? – Lneuner Oct 30 '15 at 15:57
  • When does the method `resetHighScore()` gets called? – NSNoob Oct 30 '15 at 15:58
  • 1
    also may I observe that `forKey: "highscore"` is rather curious sort of a key. Some symbols you have added or just appeared while copy pasting? – NSNoob Oct 30 '15 at 16:00
  • I'm not sure what you mean about removing ObjectForKey? – Swifty123 Oct 30 '15 at 16:17
  • Those weird symbols appeared when I posted the question. They are not actually in my code. The resetHighScore doesn't get called anywhere else. – Swifty123 Oct 30 '15 at 16:18
  • @Swifty123 he means when do you call the method where you remove the object which holds your highScore? That maybe the reason you can't access it when you return to active state – NSNoob Oct 30 '15 at 16:18
  • Okay are you using `AppDelegate`'s App State methods to perform actions as per your App's state? Like `applicationWillEnterForeground:(UIApplication *)application` , `(void)applicationDidBecomeActive:(UIApplication *)application` etc? – NSNoob Oct 30 '15 at 16:20
  • Oh! Ok. Could you possible give me an idea of how to do that? I am quite new to Swift. I love it, but on occasion it challenges me...like now! Thanks – Swifty123 Oct 30 '15 at 16:21
  • [[NSUserDefaults standardUserDefaults] synchronize]; This is in my did enter background delegate. – Swifty123 Oct 30 '15 at 16:25
  • First of all please be sure that method `resetHighScore()` is not being called anywhere where it shouldn't. When that's done, use those delegate methods to set your highscore again while entering the foreground again. It will be more helpful if you could show implementation of `viewWillDisappear` and `viewDidAppear` – NSNoob Oct 30 '15 at 16:29
  • I don't have anything in viewWillDisappear or viewDidAppear. – Swifty123 Oct 30 '15 at 16:37
  • Possible duplicate of [Swift - Saving highscore using NSUserDefaults](http://stackoverflow.com/questions/25269686/swift-saving-highscore-using-nsuserdefaults) – arled Nov 01 '15 at 17:33

3 Answers3

0

At the beginning of the code, the variable 'highscore' is reset to 0. you should use an 'if' statement to see if there is an integer in nsuserdefaults with a key of 'highscore', if not, set the value for the key 'highscore' to be 0, else, set the variable highscore to be equal to the value for the key 'highscore'. I apologise in advance for the lack of code.

R21
  • 396
  • 2
  • 12
0

It seems you never call your methods saveHighScore() or highscore(). Instead, every time you load the view, the highscore variable is set to 0.

In your lifecycle methods like viewDidLoad() or viewDidAppear(), you have to call and set it, like this: highscore = highscore(). That should get the user defaults and set the highscore variable in your view to what is saved.

In your didReceiveMemoryWarning() or better yet viewWillDisappear(), you should call saveHighScore(highscore).

That way, all your progress should always be saved and persist through closing and reopening the app.

NerdyTherapist
  • 520
  • 5
  • 15
0

NSUserDefaults is what you're looking for:

let highscore = 1000
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize()

Ref: Swift - Saving highscore using NSUserDefaults

Community
  • 1
  • 1
arled
  • 2,577
  • 6
  • 27
  • 49