Objective-C - perform a function at given time/date

≡放荡痞女 提交于 2019-12-23 17:43:34

问题


How might one get a function to be called at given time and date? For example, I would like to store some values of some variables at midnight (and clear them to zero) so they can be compared with the previous days values.

How might I accomplish this in Objective-C?

Thanks,

Jack


回答1:


You've got quite a few options, depending on what you're after.

The obvious place to start is NSTimer.

There's also the performSelector withDelay function family.

Lastly, a cool extension I sometimes use, delayed blocks.

Oh, one more - you can also setup Local Notifications.




回答2:


Use NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats: to set the time you want a method to be run.



来源:https://stackoverflow.com/questions/9028463/objective-c-perform-a-function-at-given-time-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!