本文介绍了NSTime 创建一个 timeInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的方法 updateLabel2 中有以下代码:

I have the following code in my application in the method updateLabel2:

timeLeft = [[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDate"]];

[[self timer] setText:[NSString stringWithFormat:@"Time Remaining: %f", timeLeft]]; //Set the label text

注意 timer 是一个标签.

Note that timer is a label.

及更早版本,执行以下代码:

and earlier, the following code is executed:

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastDate"];


self.repeatTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                               target:self
                                             selector:@selector(updateLabel2)
                                             userInfo:nil
                                              repeats:YES];

我的问题是 timeLeft 被设置为随机整数,这绝对不是 timeIntervals 应该是什么,并且彼此也不在一秒之内.

My problem is that timeLeft is being set to random integers which are definetly not what the timeIntervals should be, and are not within one second of each other either.

推荐答案

你的格式字符串有误:
因为 NSTimeInterval 是一个浮点类型,你需要要么

Your format-string is wrong:
Because NSTimeInterval is a floating point type, you need to either

  • 使用适当的占位符 %f 而不是 %d
  • 在调用 -[NSString stringWithFormat:] 时将 timeLeft 转换为 int.
  • use the appropriate placeholder %f instead of %d or
  • cast timeLeft to an int while calling -[NSString stringWithFormat:].

我认为这是贾斯汀所说的.

Which — I think — is, what Justin referred to.

考虑到你在那里做什么:
不应该是时间过去"吗?

Considering what you're doing there:
Shouldn't that be "Time elapsed"?

这篇关于NSTime 创建一个 timeInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:05