2011-02-09 90 views
1
//Gets the time right now 
NSDate *now = [NSDate date]; 

//Stores the difference in seconds between when the test was started and now. 
NSTimeInterval interval = [self.expires timeIntervalSinceDate:now]; 

if (interval == 0) { 
    ... 
} 

为什么条件不会成立的任何原因?检查NSTimeInterval是否等于0

谢谢。

回答

6

由于NSTimeInterval只是一个double,您可能会有浮点舍入误差。尝试使用类似的东西

if (abs(interval) < EPS) { 

其中EPS是一个足够小的常量。或者,如果您想知道秒而不是毫秒,则可以将该double截断为int。

但从您的代码判断,我认为您可能想要检查时间轴是否已经过期,而不是它在此时过期。后者的可能性非常小。这应该做的伎俩。

if (interval < 0) {