3

I am creating an app that is a game where there is a need to store the high score. I believe (correct me if I'm wrong on this, but this question made it seem this way) that user defaults are the best way to accomplish this.

Sometimes when I switch between views in my app, the values change to what they previously were. Then I will switch back to another screen, and they will go back to what they should be. This happens kind-of hit and miss, so it is hard to tell, but I think it happens most after it has been set multiple times since the app was last closed; though, I know at least sometimes it only happens after they have been set once.

I have used breakpoints to ensure that the values definitely are not being changed in the code. (I did this by putting a breakpoint where all of the user defaults are set and none of them went off even though the values changed)

The only code dealing with the user defaults is as follows:

UserDefaults.standard.set(newHighScore, forKey"highScore")

-> To set the User Defaults (where newHighScore is an integer)

let highScore = UserDefaults.standarard.value(forKey: "highScore")

-> To retrieve the values

UserDefaults.standard.synchronize()

-> Every time before and after I set or retrieve values (just to play it on the safe side and because it doesn't work so I'm overly cautious)

This question suggested restarting my mac (which I did) and then doing it only in the xcode simulator. I just did that and kept the breakpoints on all the times I set the values.

I have looked at these questions and this question seems similar, but the problem doesn't apply because it was decided that it would be fixed if the code wasn't running in a playground. My code is not running in a playground.

This question also seems similar, but it doesn't apply because the values are returning as null. Mine are just changing to a value they were previously. It is also written in objective-c, so I didn't completely understand what they did to fix it. I looked through the sources it recommended for help, but didn't see anything there that helped me either.

Thanks in advance for any help on what may be my problem. I haven't been able to find anything similar to make it seem like a bug in iOS 10 or Xcode 8 beta, but I am open to any suggestions to make my code work.

Community
  • 1
  • 1
Taylor
  • 733
  • 7
  • 18
  • Don't use valueForKey. Use integerForKey or doubleForKey. No need to force sync. – Leo Dabus Aug 04 '16 at 05:29
  • What will integerForKey return if the value is null? – Taylor Aug 04 '16 at 05:30
  • 1
    This should help http://stackoverflow.com/a/35080095/2303865 – Leo Dabus Aug 04 '16 at 05:31
  • It will return 0. If you would like to know if it is nil you can use object for key – Leo Dabus Aug 04 '16 at 05:31
  • @LeoDabus, the answer you suggested had the user defaults being set and retrieved differently, it had an extension at the top. Is that a better way to achieve this? – Taylor Aug 04 '16 at 05:36
  • It is better just because using it as an extension you can use it anywhere in your code. Just add a new swift file to your project and add it there – Leo Dabus Aug 04 '16 at 05:39
  • Then, when I set and retrieve the values, do I use the code I have been using or the code in the other answer? – Taylor Aug 04 '16 at 05:46
  • 1
    Just change the var and key name to highscore and use it the same way you use a regular var, the only difference that it will persist through launches – Leo Dabus Aug 04 '16 at 05:49
  • 1
    I'm having same problem, are you able to solve this if yes then how? Please guide. – teja_D Feb 20 '20 at 07:47

0 Answers0