将秒表转换为小时

时间:2014-02-15 13:29:22

标签: objective-c nstimer

我正在开发一款跟踪距离时间等的应用程序。 我刚刚实现了一种使用NSTimer跟踪总时间的方法,它完全按照我想要的方式工作。但我需要将总时间转换为小时数,以便我可以用它来计算卡路里和平均速度,但我遇到了麻烦!

这是我的计时器方法:

- (void) startStopWatch {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(timerController)
                                                    userInfo:nil
                                                     repeats:YES];
}

- (void)timerController {
    _seconds++;
    NSLog(@"seconds = %zd", _seconds);
    if (_seconds <10){
        NSString *secondsString = [NSString stringWithFormat:@"0%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
    }
    else{
        NSString *secondsString = [NSString stringWithFormat:@"%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
    }

    if (_seconds == 60){
        _minutes++;
        _seconds = 00;
        NSString *secondsString = [NSString stringWithFormat:@"0%li", (long)_seconds];
        _SecondsLabel.text = secondsString;
        if (_minutes <10){
            NSString *minutesString = [NSString stringWithFormat:@"0%li", (long)_minutes];
            _minutesLabel.text = minutesString;
        }
        else {
            NSLog(@"minutes = %zd", _minutes);
            NSString *minutesString = [NSString stringWithFormat:@"%li", (long)_minutes];
            _minutesLabel.text = minutesString;
        }
    }
    if (_minutes == 60){
        self.hoursLabel.hidden = NO;
        _hours++;
        _minutes = 0;
        NSString *minutesString = [NSString stringWithFormat:@"%li", (long)_minutes];
        _minutesLabel.text = minutesString;
        NSString *hoursString = [NSString stringWithFormat:@"%li", (long)_hours];
        _hoursLabel.text = hoursString;

    }
    [self UpdateTotalTime];
}

这是我将秒和分钟转换为小时的方法:

- (void) UpdateTotalTime {
    _secondsToHours = _seconds / 3600;
    _minutesToHours = _minutes / 60;
    _totalTime = (_secondsToHours + _minutesToHours + _hours);
     NSLog(@"totaltime = %zd", _totalTime);
}

问题是_totalTime没有添加任何内容,任何想法? 另外,我对Objective-C很新,所以我可能会遗漏一些非常基本的东西! 感谢

2 个答案:

答案 0 :(得分:0)

所有变量必须是双精度数,否则执行整数除法(结果为0),并转换并赋值给双变量(0.000)。

答案 1 :(得分:0)

作为如何减少代码并使其更具可读性的示例:

- (void)timerController {
    _seconds++;
    NSLog(@"seconds = %zd", _seconds);

    if (_seconds == 60) {
        _minutes++;
        _seconds = 00;
    }
    if (_minutes == 60) {
        self.hoursLabel.hidden = NO;
        _hours++;
        _minutes = 0;
        _hoursLabel.text = [NSString stringWithFormat:@"%li", (long)_hours];
    }
    _minutesLabel.text = [NSString stringWithFormat:@"%02li", (long)_minutes];
    _secondsLabel.text = [NSString stringWithFormat:@"%02li", (long)_seconds];
    [self UpdateTotalTime];
}

作为替代方案,只需使用_totaTime

- (void)timerController {
    _totalTime++;
    NSLog(@"seconds = %zd", _totalTime);

    int seconds =  _totalTime % 60;
    int minutes = (_totalTime / 60) % 60;
    int hours   =  _totalTime / 3600;

    if (hours) {
        self.hoursLabel.hidden = NO;
        _hoursLabel.text = [NSString stringWithFormat:@"%i", hours];
    }
    _minutesLabel.text = [NSString stringWithFormat:@"%02i", minutes];
    _secondsLabel.text = [NSString stringWithFormat:@"%02i", seconds];
}

答案而不是格式化评论。没有纠正任何逻辑错误。