NSString stringWithFormat "implicit conversion loses integer precision"

247 Views Asked by At

Im trying to save a high score using

 HighScore = [[NSUserDefaults standardUserDefaults]integerForKey:@"ScoreSaved"];
 Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore];

it says :

"implicit conversion loses integer precision, NSInterger(aka long) to int

-(void)EndGame
{

if (ScoreNumber > HighScore){
    HighScore = ScoreNumber;
    [[NSUserDefaults standardUserDefaults]setInteger:HighScore forKey:@"ScoreSaved"];
}

this is my first game and i am stuck how would i save a high score? thank you for taking the time to read this.

2

There are 2 best solutions below

0
trojanfoe On BEST ANSWER

You are using the wrong specifier with stringWithFormat, however getting the right one is difficult if you want to support both 32- and 64-bit targets. It's often easier to use %ld and cast the value to long:

Intro3.text = [NSString stringWithFormat:@"HighScore: %ld", (long)HighScore];
0
giorashc On

You use a wrong format specifier in your string formatting which causes an imporper conversion for the HighScore variable (NSInteger).

Use the %ld specifier which is intended for NSInteger types :

Intro3.text = [NSString stringWithFormat:@"HighScore: %ld", HighScore];