如何在 Cocoa 中有效地显示以秒为单位的时间?
我想在文本字段中显示应用程序中的时间(包括秒),非常简单。我目前所做的是创建一个每 0.1 秒运行一次的 NSTimer,用于获取系统时间并更新文本字段。这似乎是实现这一目标的一种非常低效的方法。有更好的办法吗?
I want to display the time (including seconds) in an application in a text field, pretty simple. What I currently do is create a NSTimer that runs every 0.1 seconds that gets the system time and updates the text field. This seems like a terribly inefficient way to accomplish this. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否以十分之一秒(或更精细)的分辨率显示它?
如果是这样,我认为没有问题。通常,民意调查很糟糕,因为你正在检查的内容可能没有改变,但时间不会在你身上停止。所以十分之一秒的计时器就可以了。
如果不是,则创建一个计时器,其间隔为所需的分辨率,然后获取其触发日期,将其向下舍入为所需的分辨率(例如,如果每秒更新一次,则为整秒),并将其设置为新的触发日期。然后,您的计时器只会在适合更新时钟视图的时间同时触发。
请注意,虽然时间总是在移动,但它并不总是线性移动:如果用户(或 ntpd)更改系统时钟设置,它可能会向前或向后跳跃一个小时或半小时,或者任意数量。在 Mac OS X 10.6 及更高版本上,您可以观察
NSSystemClockDidChangeNotification
,并在发生这种情况时重新调整计时器的触发日期。Are you displaying it to tenth-of-a-second (or finer) resolution?
If so, I see no problem. Usually, polling sucks because what you're checking might not have changed, but it's not like time is going to stop on you. So a tenth-of-a-second timer is fine.
If not, create a timer whose interval is the desired resolution, then get its fire date, round it down to the desired resolution (e.g., a whole second if you update every second), and set that as the new fire date. Then, your timer will only fire coincidentally with the times it's appropriate to update your clock view.
Note that while time always moves, it doesn't always move linearly: it may jump ahead or backward by an hour or half an hour, or any amount if the user (or ntpd) changes the system clock setting. On Mac OS X 10.6 and later, you can observe for the
NSSystemClockDidChangeNotification
, and re-adjust your timer's fire date when that happens.您可以使用每秒运行一次的 NSTimer,然后检查触发时间并确保它恰好在一秒钟的开始时间。您可以使用
initWithFireDate:interval:target:selector:userInfo:repeats:
或在创建计时器后使用setFireDate:
适当设置下一次调用时间:然后您将确保计时器触发尽可能接近所需的时间,并且不用担心计时器滞后,因为它会在每次触发后使用您提供的预期触发日期和间隔自行重置,因此它总是会尝试在精确的秒触发(但只要运行循环能够做到这一点就会触发)。
How about you use NSTimer that runs every second and then you check the firing time and make sure it is exactly on the start of a second. You can use
initWithFireDate:interval:target:selector:userInfo:repeats:
or set the next invocation time appropriately usingsetFireDate:
after the timer was created:Then you'll be sure that the timer fires as close to the needed time as possible and don't worry about the timer lagging behind because it resets itself after each firing using intended firing date and interval you provided, so it will always try to fire at exact second (but will fire whenever the run loop will be able to do that).
我不确定还有什么会更有效。只要计时器的回调没有做任何过于密集的事情,您就可能接近最佳状态。
I'm not sure what else would be more efficient. As long as the callback for your timer is not doing anything too intensive, you're probably close to optimal.
使用
gettimeofday(2)
怎么样?它返回许多准确的时间信息以供使用。How about using
gettimeofday(2)
? It returns much exact time information to use.