比较 nstimer 选择器内的日期

发布于 2024-10-16 05:22:54 字数 1124 浏览 1 评论 0原文

我正在编写一个倒计时器应用程序,就像 iPhone 时钟应用程序中的计时器选项卡一样。现在我在比较日期、“现在”和“未来日期”时遇到困难。 所有变量都是综合的并且是非原子的并保留。 我现在有这个代码。

- (IBAction)startTimer:(id)sender {
    NSLog(@"startTimer");
    pickerView.hidden = YES;
    labelView.hidden  = NO;
    now = [[NSDate alloc] init];
    futureDate = [[NSDate alloc] initWithTimeInterval:picker.countDownDuration sinceDate:now];
    NSLog(@"Dates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    timerLabelUpdater = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(labelUpdater) userInfo:nil repeats:YES];
}

- (void)labelUpdater {
    if ([now timeIntervalSinceDate:futureDate] < 0.0) {
        NSLog(@"YES\nDates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    } else {
        NSLog(@"NO\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    }
}

调试器信息: 2011-02-08 16:46:02.449 应用程序[22504:207] 启动定时器 2011-02-08 16:46:02.451 应用程序[22504:207] 日期。 现在 = (2011-02-08 18:46:02 +0000)
未来日期 (2011-02-08 18:47:02 +0000) 2011-02-08 16:46:03.451 App[22504:207] 是

并且它一直给我“永远”是的。 但如果你看到可变的时间,与我的时钟时间相比,它们是未来+2小时。这是错误吗?

I am writing a countdowtimer application like the timer tab in the clock app of the iPhone. Right now I am having trouble comparing to dates, the 'now' and 'futureDate'.
all the variables are synthesized and are nonatomic and retain.
I have this code right now.

- (IBAction)startTimer:(id)sender {
    NSLog(@"startTimer");
    pickerView.hidden = YES;
    labelView.hidden  = NO;
    now = [[NSDate alloc] init];
    futureDate = [[NSDate alloc] initWithTimeInterval:picker.countDownDuration sinceDate:now];
    NSLog(@"Dates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    timerLabelUpdater = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(labelUpdater) userInfo:nil repeats:YES];
}

- (void)labelUpdater {
    if ([now timeIntervalSinceDate:futureDate] < 0.0) {
        NSLog(@"YES\nDates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    } else {
        NSLog(@"NO\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
    }
}

Debugger info:
2011-02-08 16:46:02.449 App[22504:207] startTimer
2011-02-08 16:46:02.451 App[22504:207] Dates.
Now = (2011-02-08 18:46:02 +0000)
futureDate (2011-02-08 18:47:02 +0000)
2011-02-08 16:46:03.451 App[22504:207] YES

And it stay giving me yes "forever".
But if you see the variable hours, they are +2h in the future compared from my clock time. Is this the bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2024-10-23 05:22:54

抱歉,如果测试了错误的日期,比较变量 now 和 futureDate 的时间间隔将永远相同。我在测试中将 now 变量替换为 [[NSDate alloc] init] 。现在代码可以工作了。

if ([[[NSDate alloc]init] timeIntervalSinceDate:futureDate] < 0.0) {
    NSLog(@"YES\nDates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
} else {
    NSLog(@"NO\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
}

抱歉这个愚蠢的问题。

Sorry my if was testing the wrong date, comparing the variable now and futureDate the time interval will be the same forever. I replaced the now variable for a [[NSDate alloc] init] in the test. Right now the code works.

if ([[[NSDate alloc]init] timeIntervalSinceDate:futureDate] < 0.0) {
    NSLog(@"YES\nDates.\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
} else {
    NSLog(@"NO\nNow = (%@)  \nfutureDate (%@)", now, futureDate);
}

sorry for the stupid question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文