如何在 Cocoa 中有效地显示以秒为单位的时间?

发布于 2024-08-09 13:21:27 字数 113 浏览 4 评论 0原文

我想在文本字段中显示应用程序中的时间(包括秒),非常简单。我目前所做的是创建一个每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黎夕旧梦 2024-08-16 13:21:27

您是否以十分之一秒(或更精细)的分辨率显示它?

如果是这样,我认为没有问题。通常,民意调查很糟糕,因为你正在检查的内容可能没有改变,但时间不会在你身上停止。所以十分之一秒的计时器就可以了。

如果不是,则创建一个计时器,其间隔为所需的分辨率,然后获取其触发日期,将其向下舍入为所需的分辨率(例如,如果每秒更新一次,则为整秒),并将其设置为新的触发日期。然后,您的计时器只会在适合更新时钟视图的时间同时触发。

请注意,虽然时间总是在移动,但它并不总是线性移动:如果用户(或 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.

夏末的微笑 2024-08-16 13:21:27

您可以使用每秒运行一次的 NSTimer,然后检查触发时间并确保它恰好在一秒钟的开始时间。您可以使用 initWithFireDate:interval:target:selector:userInfo:repeats: 或在创建计时器后使用 setFireDate: 适当设置下一次调用时间:

NSTimeInterval interval = [[timer fireDate] timeIntervalSinceReferenceDate];
NSTimeInterval nextFire = ceil(interval);
[timer setFireDate: [NSDate dateWithTimeIntervalSinceReferenceDate: nextFire];

然后您将确保计时器触发尽可能接近所需的时间,并且不用担心计时器滞后,因为它会在每次触发后使用您提供的预期触发日期和间隔自行重置,因此它总是会尝试在精确的秒触发(但只要运行循环能够做到这一点就会触发)。

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 using setFireDate: after the timer was created:

NSTimeInterval interval = [[timer fireDate] timeIntervalSinceReferenceDate];
NSTimeInterval nextFire = ceil(interval);
[timer setFireDate: [NSDate dateWithTimeIntervalSinceReferenceDate: nextFire];

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).

冷了相思 2024-08-16 13:21:27

我不确定还有什么会更有效。只要计时器的回调没有做任何过于密集的事情,您就可能接近最佳状态。

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.

裂开嘴轻声笑有多痛 2024-08-16 13:21:27

使用gettimeofday(2)怎么样?它返回许多准确的时间信息以供使用。

How about using gettimeofday(2)? It returns much exact time information to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文