ios - NSDate 返回一个负的 NSTimeInterval

标签 ios objective-c grand-central-dispatch nstimer

我有以下代码:

- (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/

相关文章:

iphone - 切换选项卡(最初)非常慢 - 将功能移出 viewDidLoad?

ios - 我如何在 ios 中使用 UIBezierPath 绘制直线

ios - SceneKit 中的相机旋转

ios - 捕获单元格的索引值,但仅来自屏幕上的可见单元格

objective-c - iOS 5.1 + PortraitMode 中的 UISplitViewController + MasterController 中的 UIActionSheet = 断言失败

ios - 在后台线程中上传大图像。

multithreading - 在 GCD(大中央调度)中创建 dispatch_queues 多少是太多了?

objective-c - iOS 5 - 有没有办法在 UITextView 中禁用自动填充但保留拼写检查(红色下划线)?

ios - UIAlertController addTextFieldWithConfigurationHandler使用全局变量

swift - 解决 iOS Swift 中的数据争用