我有以下代码:
- (void)pointsStartedDisplaying {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_async(dispatch_get_main_queue(), ^{
self.timeStartedDisplayingPoint = [NSDate date];
self.stopButton.enabled = YES;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateElapsedTime:) userInfo:nil repeats:YES];
});
});
}
- (void)updateElapsedTime:(NSTimer *)timer {
NSTimeInterval elapsedTimeDisplayingPoints = [self.timeStartedDisplayingPoint timeIntervalSinceDate:[NSDate date]];
self.elapsedTimeValueLabel.text = [NSString stringWithFormat:@"%f seconds", elapsedTimeDisplayingPoints];
}
但是,elapsedTimeDisplayingPoints
的值非常准确,只是每次都是负值。为什么它会返回 -3、-4、-5、
等?
最佳答案
If the receiver is earlier than anotherDate, the return value is negative .你的例子就是这种情况。您可以使用 fabs() 获得正值。
关于ios - NSDate 返回一个负的 NSTimeInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488817/