首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mac os x上定时执行函数?

如何在mac os x上定时执行函数?
EN

Stack Overflow用户
提问于 2013-02-21 18:55:03
回答 1查看 155关注 0票数 0

我为mac写了一个简单的应用程序,使用系统时间来计算一些数据,我想在时钟到达12:00的时候运行一个特定的例程

有没有什么系统api可以解决这个问题?

我该怎么做呢?

实际上,我在我的应用程序中有一个状态栏项目,显示任何选定日历中的当前日期,如公历和伊斯兰,我希望这个值将与系统时间同步

EN

回答 1

Stack Overflow用户

发布于 2013-02-22 01:44:40

如果您的例程是object- routine:(NSTimer *)timer方法

代码语言:javascript
复制
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate]; // Current time
NSTimeInterval then = ((int)now + 86400) % 86400; // Next 12am
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:then];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:date interval:86400 target:object @selector(routine:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15000430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档