NSUserDefaults standardUserDefaults setInteger for long long int
I am trying to create a high score system in my iPhone app that allows long long int to be stored in a high score save file because I expect high scores to be greater than a normal integer. But all I could find in terms of code was this:
if (ScoreNumber > HighScore) {
HighScore = ScoreNumber;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:@"HighScoreSaved"];
}
HighScoreSaved will not save correctly, as soon as the indicator number reaches a value greater than 2,147,483,647 or the maximum integer value for a 4-byte integer, HighScoreSaved will store a completely different integer value. Here's what I have in my ViewController.h in terms of data types:
int y, RandomPosition;
long long int ScoreNumber, HighScore;
BOOL Start;
To reiterate and make sure I'm as clear as possible, I'm looking for a way to keep the long long numeric score in objective-c. Any input would be very much appreciated and this question may sound silly, but this is my first app using xcode and objective-c so everything will be fine. Thank.
source to share
Instead of splitting the count in two yourself, keep the value wrapped in NSNumber
, for example:
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithLongLong:HighScore]
forKey:@"HighScoreSaved"
];
Get a high score like this:
HighScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScoreSaved"] longLongValue];
source to share