简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM