views:

72

answers:

1

I am have created a timer where I convert the remaining time to a string with a format of:"mm:ss" and when the string value of the time is 00:00 I would like to invalidate the timer. For some reason it doesn't work, even when I log the remaining time I see the the value has the correct format so I don't know why my "if" branch in "countTime:" is never entered.

Can anyone help?

-(id) init {
    if(self = [super init]) {
        NSDate *date = [[NSDate alloc] init];
        self.intervalLength = date;
        [date release];

        NSString *timeRem = [[NSString alloc]init];
        self.timeRemaining = timeRem;
        [timeRem release];


        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"mm:ss"];
        notification = [NSNotification notificationWithName:@"timer" object:self];
        notificationForEnd = [NSNotification notificationWithName:@"timerFinished" object:self];
        NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
        [info setObject:formatter forKey:@"format"];
        [info setObject:notification forKey:@"notification"];
        [info setObject:notificationForEnd forKey:@"notificationEnd"];
        NSTimer *timer = [[NSTimer alloc] init];
        timer = [[NSTimer alloc] init];
        timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countTime:) userInfo:info repeats:YES];
        self.intervalTimer = timer;
    }
    return self;
}

-(void) startIntervalCountDown {
    runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.intervalTimer forMode:NSDefaultRunLoopMode];
}

-(void) countTime:(NSTimer*)timer {
    if(self.timeRemaining == @"00:00") {
        [timer invalidate];
        timer = nil;
        [[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notificationEnd"]];
    }
    d = [self.intervalLength dateByAddingTimeInterval: -1.0];
    self.intervalLength = [d copy];
    self.timeRemaining = [[[timer userInfo] objectForKey:@"format"] stringFromDate:d];  
    [[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notification"]];
A: 

You're comparing pointer equality rather than string equality. You want

[self.timeRemaining isEqualToString:@"00:00"]
Seamus Campbell