[英]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...这是调用游戏结束功能时我的 .m 文件中的内容...
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...如果我将 highscorenumber 更改为 int64_t 警告出现在这里...
[[NSUserDefaults standardUserDefaults] setInteger:highscorenumber forKey:@"highscoresaved"];
The reason i am using int64_t for score is to use GameKit (GameCenter).我使用 int64_t 得分的原因是使用 GameKit (GameCenter)。
NSInteger and long are always pointer-sized. NSInteger 和 long 总是指针大小的。 That means they're 32-bits on 32-bit systems, and 64 bits on 64-bit systems.这意味着它们在 32 位系统上是 32 位,在 64 位系统上是 64 位。
Under mac os uint64_t defined as在 mac os 下 uint64_t 定义为
typedef unsigned long long uint64_t;
So, i recommend you to change highscorenumber to NSUInteger and save it to NSUserDefaults as因此,我建议您将 highscorenumber 更改为 NSUInteger 并将其保存到 NSUserDefaults 作为
[[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
?为什么使用NSInteger
来存储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如果你想让它与 GameKit 兼容,它应该是 64 位的,你需要使用 int64_t 作为你的高分属性
int64_t highscorenumber;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.