我正在写一个countdowtimer应用程序,比如iPhone的时钟应用程序中的计时器选项卡。现在我在与日期,'现在'和'futureDate'进行比较时遇到了麻烦。 所有变量都是合成的并且是非原子的并且保留。 我现在有这个代码。
- (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 App [22504:207] startTimer
2011-02-08 16:46:02.451 App [22504:207]日期。
现在=(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]是
它一直给我“永远”。 但是如果你看到变量小时数,那么与我的时钟时间相比,它们将来会是+ 2小时。这是错误吗?
答案 0 :(得分:0)
对不起我的if测试错误的日期,比较现在和futureDate的变量,时间间隔将永远相同。我在测试中替换了[[NSDate alloc] init]的now变量。现在代码可以运行。
if ([[[NSDate alloc]init] timeIntervalSinceDate:futureDate] < 0.0) {
NSLog(@"YES\nDates.\nNow = (%@) \nfutureDate (%@)", now, futureDate);
} else {
NSLog(@"NO\nNow = (%@) \nfutureDate (%@)", now, futureDate);
}
抱歉这个愚蠢的问题。