0

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).

Emmanuel Amaro
  • 157
  • 1
  • 1
  • 8

4 Answers4

1

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"];
Doro
  • 2,413
  • 2
  • 14
  • 26
  • ok, and when i want to load it back would i do it like this. highscorenumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"highscoresaved"]; – Emmanuel Amaro Jun 24 '15 at 19:11
  • no, you should cast it to NSNumber after valueForKey: selector. Remember - you should use exactly the same method to set and get data: for example, if you set data throw 'setValue', you must get it like 'valueForKey', if you set throw 'setInteger: forKey' call 'integerForKey:' – Doro Jun 25 '15 at 06:02
0

Try this:

highscorenumber = @(score);
João Pereira
  • 3,545
  • 7
  • 44
  • 53
0

Try this

highscorenumber = [score integerValue];
Dharman
  • 30,962
  • 25
  • 85
  • 135
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
0

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;
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47