简体   繁体   English

如何正确地将NSNumber转换为long long int?

[英]How to cast NSNumber to long long int correctly?

im trying to cast NSNumber to long long int but I'm getting unexpected values.. What am I doing wrong? 我试图将NSNumber转换为long long int但我得到了意想不到的值..我做错了什么?

NSNumber *number = [[NSNumber alloc] initWithInt:60];
if ([self isTimeOver:number]) {
    [self sendPushTest];
}

-(BOOL)isTimeOver: (NSNumber*) interval {
    long long int theInterval = (long long int)interval;
    NSLog(@"THE INTERVAL %lld",theInterval); // I get -5764607523034233918 here.
}

Use -longLongValue since long long int and long long are the same. 使用-longLongValue因为long long intlong long是相同的。

long long int theInterval = [interval longLongValue];

You should never try just casting NSNumber s to primitive types (doing so will result in the garbage number you got). 你永远不应该只尝试将NSNumber转换为原始类型(这样做会导致你得到的垃圾编号)。 Take a look at the documentation for more info. 查看文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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