2014-09-23 39 views
-1

我在与分配一个NSTimeInterval值一个NSNumber块中投NSTimeInterval为NSNumber的

这里块内的困难是什么,我到目前为止有:

[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语句为真时,我需要为变量lastTime指定一个新值 - 问题是我似乎无法弄清楚如何为变量赋值一个NSTimeInterval值lastTime是NSNumber类型的。我感到非常困惑,因为我读的所有内容都告诉我,两者都只是双打。有任何想法吗?

+0

NSTimeInterval只是标量/基元'双'类型的别名,而NSNumber是一个类。您不能在Objective-C中的标量和对象之间“投射”。 – 2014-09-23 23:10:48

回答

3

您需要了解seconds是原始类型(NSTimeInterval - 确实是double)。 lastTime是班级类型NSNumber

要创建一个NSNumber从一个原始的号码类型,你可以这样做:

lastTime = @(seconds); 

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

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

NSNumber *lastTime = nil; 

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

顺便说一句 - 这些都与使用块有关。

+0

谢谢 - 很好的答案 – 2014-09-23 23:06:53