开发者

Comparing dates inside a nstimer selector

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 n开发者_C百科ow.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜