0

我在方法的应用程序中有以下代码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 应该是的,并且彼此也不在一秒内。

4

1 回答 1

0

您的格式字符串错误:
因为NSTimeInterval是浮点类型,您需要

  • 使用适当的占位符%f代替%dor
  • timeLeft调用时强制转换为int -[NSString stringWithFormat:]

其中——我认为——就是贾斯汀所说的。

在旁边

考虑到你在那里做什么:
那不应该是“时间流逝”吗?

于 2011-02-17T16:05:24.130 回答