NSUserDefaults won't save my high score permanently - ios

my NSUserDefaults doesn't save my game high score properly. When am playing(testing) the game it works ok but when i quit(terminate) and restart or when i transition from scene to scene my high score goes back to 0. can anyone help me fix this issue? thank in advance.
var score = 6
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
//Check if score is higher than NSUserDefaults stored value and change NSUserDefaults stored value if it's true
if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
NSUserDefaults.standardUserDefaults().synchronize()
}
NSUserDefaults.standardUserDefaults().integerForKey("highscore")

The problem is that this statement, which appears twice, does nothing at all:
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
It fetches the "highscore" key's value from user defaults, but immediately throws it away, because you are not assigning the result to anything, like this:
let highscore = NSUserDefaults.standardUserDefaults().integerForKey("highscore")
Thus, even though you are successfully storing the high score in user defaults, you are never retrieving it - so there is no basis for your claim that it is not being saved, because you have given yourself no way of knowing whether or not is has been saved.

Related

IOS app keeps crashing because of highscore

After trying to submit my game to the app store, my app got rejected because of some bug that's working on an iPad 2. I tried to find the problem, and it was because of some high score bug. This happens when the player gets 0 for the first time, right on the scene that shows your score and high score. Here's the code:
var highScoreDefault = NSUserDefaults.standardUserDefaults()
//Right below is the problem
highScore = highScoreDefault.valueForKey("highScore") as NSInteger
Here's the error I get if I get 0 for the first time:
EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP,subcode=0x0)
I keep trying to find a different but a simple way to add the high score, but I can't find it. Please help!
Note: I'm running on Xcode 6.2 in Swift, and this happened on all iOS simulators.
Without more code... Try just make if let statement
something like:
if let value = highScoreDefault.valueForKey("highScore") as? NSInteger {
highScore = value
} else {
highScore = 0
}

How to use NSUserDeafults to store integer (Swift in iOS) [duplicate]

This question already has answers here:
Swift - Saving highscore using NSUserDefaults
(5 answers)
Closed 6 years ago.
I am searching for an efficient way to store a "high score" integer in this simple game I am building with Swift in iOS. I want it to keep track of it even if the player shuts down the app, closes it, and restarts it. How should I use the NSUserDefaults function to save this data? Thanks in advance! :)
//TO SAVE INTO USER DEFAULT
NSUserDefaults.standardUserDefaults().setInteger(1234, forKey: "highScore")
//TO RETRIEVE FROM USER DEFAULT
var highScore = NSUserDefaults.standardUserDefaults().integerForKey("highScore")
Save that highscore like this:
let highscore = 20
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize() // it is not necessary
When you want to get the info, you have to "read" the highscore from the dictionary like this:
if let highscore = userDefaults.valueForKey("highscore") {
// do something here when a highscore exists
}
else {
// no highscore exists
}

My Highscore amount keeps going up and down. Swift SpriteKit

I have a spritekit game which I have a highscore in. It uses NSUser Default. But I get the highscore 2, and then I close the app completely, and then open it it shows my highscore 2, and then get one as a score. It remains as 2. However, I close the app again and open it, it shows the highscore 1. Why does it do this? This is my code. Does the if condition not work? Note: This is just narrowed down to Highscore code.
import SpriteKit
//In the DidMoveToView function
if let Highscore1 = defaults.stringForKey("Highscore"){
HighScoreLabel.text = "HIGHSCORE: \(Highscore1)"
}
//In the touches began func
//Making what happens when the User Fails and a new highscore is achieved
if Score > highscore {
defaults.setObject("\(Score)", forKey: "Highscore")
}
Thank you in advance
The issue is you are reading the highscore from NSUserDefaults and showing it in the HighScoreLabel. But you didn't assigned/stored the value in highscore variable, because of that it remains at 0. That makes the following condition true when you open the app and plays for the first time:
if Score > highscore {
defaults.setObject("\(Score)", forKey: "Highscore")
}
You need to change the high score reading part like:
if let Highscore1 = defaults.stringForKey("Highscore") {
HighScoreLabel.text = "HIGHSCORE: \(Highscore1)"
// Storing current high score to variable
highscore = Int(Highscore1)
}

HighScore Not Displaying In Menu Scene

I have an app with a score and highscore system. I can display the highscore within the gameplay scene, but it shows up as 0 in the menu scene. In the gameplay scene I create the variable
var highScore = 0
then I say
if (score > highscore) { score = highscore
I display it inside that scene, I go back to the menu scene and it accesses it by saying
highscoreLabel = actionscene.highscore
(actionscene being the name of the file for my game play scene.) Since the
original value of highscore is 0, it displays 0. How can I fix this or program it in a different way?
You can use NSUserDefaults as an easiest solution. In the GameScene:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(10, forKey: "highscore")
defaults.synchronize()
In another scene (MenuScene) :
let defaults = NSUserDefaults.standardUserDefaults()
let highscore = defaults.integerForKey("highscore")
Also note that the synchronize() method, is automatically invoked at periodic intervals, and it keeps in-memory cache in sync with a user’s defaults database.
Ideally, you would let the system to worry about when the persistent storage is updated, but there are cases when you want to do it by yourself and call synchronize() manually:
Because this method is automatically invoked at periodic intervals,
use this method only if you cannot wait for the automatic
synchronization (for example, if your application is about to exit) or
if you want to update the user defaults to what is on disk even though
you have not made any changes.

using NSUserDefaults to display score

I am using NSUserDefaults to display the score from my GameScene on my resetViewController. The problem is that if the user doesn't score it will display the players last score.
In GameScene
var score: Int = 0
var scoreLabel = SKLabelNode()
let defaults = NSUserDefaults.standardUserDefaults()
score++
scoreLabel.text = "\(score)"
defaults.setObject(scoreLabel.text, forKey: "scoring")
In resetViewController
var score = defaults().stringForKey("scoring")
YourScore.text = score
I'm sure it's something simple but I can't seem to figure it out
When you need to score to reset, just call
NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "scoring")
removeObjectForKey: removes the value from NSUserDefaults. You could also set it to zero. Or just not use NSUserDefaults -- there's not much point in using it unless you need to persist the value between restarts of your app.
I suggest saving the integer, not the string. That way the default will be zero, and you can easily reset the score by setting the key to zero.
NSUserdefaults.standardUserDefaults().IntegerForKey("score")
NSUserdefaults.standardUserDefaults().setInteger(0, forKey: "score")

Resources