user3457898
user3457898

Reputation: 13

NSString stringWithFormat "implicit conversion loses integer precision"

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.

Upvotes: 0

Views: 248

Answers (2)

trojanfoe
trojanfoe

Reputation: 122401

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];

Upvotes: 4

giorashc
giorashc

Reputation: 13713

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];

Upvotes: 0

Related Questions