NSTime 创建一个时间间隔
我的应用程序的方法 updateLabel2
中有以下代码:
timeLeft = [[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDate"]];
[[self timer] setText:[NSString stringWithFormat:@"Time Remaining: %f", timeLeft]]; //Set the label text
请注意,计时器是一个标签。
更早之前,执行以下代码:
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastDate"];
self.repeatTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(updateLabel2)
userInfo:nil
repeats:YES];
我的问题是 timeLeft 被设置为随机整数,这绝对不是 timeIntervals 应该设置的值,并且彼此之间也不在一秒之内。
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
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];
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的格式字符串错误:
由于
NSTimeInterval
是浮点类型,因此您需要%f
而不是%d
或timeLeft<调用
-[NSString stringWithFormat:]
时将 /code> 转换为 int。我认为这就是贾斯汀所指的。
除了
考虑你在那里做什么:
这不应该是“时间已过去”吗?
Your format-string is wrong:
Because
NSTimeInterval
is a floating point type, you need to either%f
instead of%d
ortimeLeft
to an int while calling-[NSString stringWithFormat:]
.Which — I think — is, what Justin referred to.
Aside
Considering what you're doing there:
Shouldn't that be "Time elapsed"?