简体   繁体   中英

How can i equal a int to a int64_t with out getting the conversion loses integer precision warning?

I am getting the implicit conversion loses interger precision warning and need help finding a solution. I have seen similar problems but have not found a solution for my problem yet. This are the integers that are declared...

@property (nonatomic) int64_t score;
NSInteger highscorenumber;

this is whats in my .m file when game over function is called...

    if (score > highscorenumber) {
    highscorenumber = score;
    [[NSUserDefaults standardUserDefaults] setInteger:highscorenumber forKey:@"highscoresaved"];
    [self clapsound];
}
else{
    highscore.text = [NSString stringWithFormat:@"High Score: %li",(long)highscorenumber];
}

the warning come up in this part

        highscorenumber = score;

if i change the highscorenumber to a int64_t the warning comes up here...

        [[NSUserDefaults standardUserDefaults] setInteger:highscorenumber forKey:@"highscoresaved"];

The reason i am using int64_t for score is to use GameKit (GameCenter).

NSInteger and long are always pointer-sized. That means they're 32-bits on 32-bit systems, and 64 bits on 64-bit systems.

Under mac os uint64_t defined as

typedef unsigned long long   uint64_t;

So, i recommend you to change highscorenumber to NSUInteger and save it to NSUserDefaults as

[[NSUserDefaults standardUserDefaults] setValue:@(highscorenumber) forKey:@"highscoresaved"];

EDIT:

Getting value back:

NSNumber *highscorenumber = (NSNumber*)[[NSUserDefaults standardUserDefaults] valueForKey:@"highscoresaved"];

尝试这个:

highscorenumber = @(score);

尝试这个

highscorenumber = [score integerValue];

Why do you use NSInteger for storing highscorenumber ? If you want it to be compatible with GameKit it should be 64-bit, you need to use int64_t for your highscore property

int64_t highscorenumber;

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