Objective c 将秒表转换为小时
我正在开发一个追踪距离、时间等信息的应用程序。 我刚刚使用NSTimer实现了一种跟踪总时间的方法,它的工作方式正是我所希望的。但是我需要把总时间转换成小时,这样我就可以用它来计算卡路里和平均速度,但是我有麻烦了 以下是我的计时器方法:Objective c 将秒表转换为小时,objective-c,nstimer,Objective C,Nstimer,我正在开发一个追踪距离、时间等信息的应用程序。 我刚刚使用NSTimer实现了一种跟踪总时间的方法,它的工作方式正是我所希望的。但是我需要把总时间转换成小时,这样我就可以用它来计算卡路里和平均速度,但是我有麻烦了 以下是我的计时器方法: - (void) startStopWatch { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
- (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];
}
问题是,totalTime没有添加任何内容,有什么想法吗?
另外,我对Objective-C非常陌生,所以我可能缺少一些非常基本的东西!
非常感谢所有变量都必须是双精度的,否则将执行整数除法(结果为0),并将其转换并分配给双精度变量(0.000)。仅作为如何减少代码并使其更具可读性的示例:
- (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];
}
作为替代,只需使用\u 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];
}
与格式注释相反的答案。没有纠正任何逻辑错误。您的_secondsToHours。。。声明的变量?提示:如果需要,只需使用
%02li
来获取两个前导为零的数字,而不是if/else语句。学习文档可以保存代码行。提示:要自动更正代码缩进,请选择代码,然后键入control-i。威利·杰克·德格尔(Willie Jack Degel)对“如果你不能阅读代码,你就无法修复代码”的解释。@Merlevede我已经将“secondsToHours”声明为“@property double secondsToHours;”@Zaph感谢这些提示非常有用一旦我改变了我的秒数翻倍一切都很好。谢谢。@user3254994我希望您能将我的答案标记为正确,因为这正是解决您问题的方法。
- (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];
}