比较 nstimer 选择器内的日期
我正在编写一个倒计时器应用程序,就像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,如果测试了错误的日期,比较变量 now 和 futureDate 的时间间隔将永远相同。我在测试中将 now 变量替换为 [[NSDate alloc] init] 。现在代码可以工作了。
抱歉这个愚蠢的问题。
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.
sorry for the stupid question.