检查NSTimeInterval是否等于0

Luc*_*uke 2 iphone objective-c

//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) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

任何原因导致这种情况不正确?

谢谢.

Nik*_*bak 7

由于NSTimeInterval只是a double,您可能会出现浮点舍入错误.尝试使用类似的东西

if (abs(interval) < EPS) {
Run Code Online (Sandbox Code Playgroud)

哪里EPS是一个足够小的常数.

或者,如果您想知道秒而不是毫秒,则可以将该双截断为int.

但从您的代码判断,我认为您可能想要检查时间轴是否已经过期,而不是它在此时间到期.后者的概率非常小.这应该可以解决问题.

if (interval < 0) {
Run Code Online (Sandbox Code Playgroud)