简体   繁体   English

如何从NSNumber中获取整数

[英]How do I get an integer out of NSNumber

I have this code, I save total second counting and load it in result view controller. 我有这段代码,我保存了总秒数并将其加载到结果视图控制器中。 and in my result view controller, I put this code, 然后在结果视图控制器中放入以下代码,

- (void) counting
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *loadSecond = [defaults objectForKey:@"saveSecond"];
    NSLog(@"last save is %@", loadSecond); // in here, log is displaying 30 seconds
    scratch = fmod(secondCount,3600);
    hour =  secondCount / 3600;
    minute = scratch / 60;
    second = fmod(scratch,60);
    labelRecord.text = [NSString stringWithFormat:@"%d:%d:%d", hour , minute, second];
}

From above, NSLog is displaying my last save counting for 30 seconds, and it is right but how do i change that 30 seconds to secondCount? 从上面,NSLog显示我的上一次保存计数为30秒,这是正确的,但是我如何将30秒更改为secondCount? because i want use it in my labelRecord. 因为我想在我的labelRecord中使用它。

Change this line 更改此行

NSNumber *loadSecond = [defaults objectForKey:@"saveSecond"];

to

int loadSecond = [[defaults objectForKey:@"saveSecond"] intValue];

This assumes that the object for the key saveSecond is a NSNumber . 假定键saveSecond的对象是NSNumber

A quick look in the docuemntation would show that there's an NSUserDefaults method that returns an integer: 快速浏览文档会发现有一个NSUserDefaults方法返回一个整数:

NSInteger loadSecond = [defaults integerForKey:@"saveSecond"]; 

NSInteger is just an int . NSInteger只是一个int There's also NSUInteger which is unsigned. 还有NSUInteger是未签名的。

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

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