0

残り時間を「mm:ss」の形式の文字列に変換するタイマーを作成しました。時間の文字列値が00:00の場合、タイマーを無効にします。何らかの理由で機能しません。残り時間をログに記録しても、値の形式が正しいことがわかります。そのため、「countTime:」の「if」ブランチが入力されない理由がわかりません。

誰か助けてもらえますか?

-(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"]];
4

1 に答える 1

2

文字列の同等性ではなく、ポインタの同等性を比較しています。あなたが欲しい

[self.timeRemaining isEqualToString:@"00:00"]
于 2010-08-04T02:13:39.683 に答える