简体   繁体   English

我怎样才能将 int 等于 int64_t 而不得到转换丢失整数精度警告?

[英]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.

相关问题 AFURLConnectionOperation.m隐式转换失去整数精度:'int64_t'(又名'long long')到'NSInteger'(又名'int') - AFURLConnectionOperation.m Implicit conversion loses integer precision: 'int64_t' (aka 'long long') to 'NSInteger' (aka 'int') 我收到警告隐式转换失去整数精度 - I am getting a warning implicit conversion loses integer precision 隐式转换将整数精度'NSUInteger'(aka'unsigned long')丢失为'int'警告? - implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning? 你如何将int转换为int64_t - How do you convert int into int64_t 隐式转换失去整数精度:'long long'到'NSInteger'(又名'int') - Implicit conversion loses integer precision: 'long long' to 'NSInteger' (aka 'int') 隐式转换失去整数精度:'unsigned long'到'int'-错误 - Implicit conversion loses integer precision: 'unsigned long' to 'int' - Error 如果iOS是32位操作系统,我们如何使用uint64_t和int64_t? - If iOS is a 32 bit OS, how can we be using uint64_t and int64_t? ios:如何将NSString转换为int64_t? - ios: how to convert a NSString to int64_t? 如何修复隐式转换丢失整数精度警告 - How to fix Implicit conversion loses integer precision warning 关于隐式转换失去整数精度的警告 - Warning about Implicit conversion loses integer precision
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM