在块中将NSTimeInterval作为NSNumber进行转换

时间:2014-09-23 23:01:17

标签: ios casting nsnumber nstimeinterval

我在一个块中遇到困难,并为NSNumber分配了NSTimeInterval值

这是我到目前为止所做的:

[player addPeriodicTimeObserverForInterval:CMTimeMake(3, 10) queue:NULL usingBlock:^(CMTime time){
            NSTimeInterval seconds = CMTimeGetSeconds(time);
            NSNumber *lastTime = 0;
            for (NSDictionary *item in bualadhBos) {
                NSNumber *time = item[@"time"];
                if ( seconds > [time doubleValue] && seconds > [lastTime doubleValue]) {

                    lastTime = [seconds doubleValue];// this line causes difficulties
                    NSString *str = item[@"line"];
                    break;
                }; }

          }

我跟踪NSNumber中的时间,当if语句为true时,我需要为变量lastTime分配一个新值 - 问题是我似乎无法弄清楚如何将包含在秒内的NSTimeInterval值分配给类型为NSNumber的变量lastTime。我真的很困惑,因为我读到的一切都告诉我,两者都只是双打。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您需要了解seconds是基本类型(NSTimeInterval - 真正double)。 lastTime是类类型NSNumber

要从原始数字类型创建NSNumber,您可以执行以下操作:

lastTime = @(seconds);

这是lastTime = [NSNumber numberWithDouble:seconds]的现代语法。

NSNumber *lastTime = 0;在技术上是正确的但不是真的,这是误导。你想要:

NSNumber *lastTime = nil;

不要将原始数字与NSNumber和对象混淆。

BTW - 这与使用块无关。