简体   繁体   中英

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

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). Take a look at the documentation for more info.

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