正在听更改为本周吗?
我正在 OS X 中为自己测试一些东西,我想知道最好的方法是什么。
我有一个方法,它返回当前周数。
-(NSInteger) getWeekNumber {
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:date];
return [components week];
}
由于这种情况每周只会发生一次,所以有一个线程每秒左右刷新一次我的标签似乎很愚蠢,但我希望标签值在周数更改的那一刻发生更改。
我还想用当前时间更新标签,实际上是每秒一次,或每分钟一次,具体取决于设置。那应该只是一个每秒运行一次的线程吗?
I'm testing a few things for my self in OS X, and I would like to know what the best way to do this would be.
I have a method, that returns the current week number.
-(NSInteger) getWeekNumber {
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:date];
return [components week];
}
Since this will only happen once a week it seems stupid to have a thread that will refresh my label once every second or so, but I want the label value to change at the exact second the week number change.
I also want to update a label with the current time, that will actually be once a second, or once a minute depending on settings. Should that just be a thread that runs once a second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要使用线程来执行这些简单的任务。
NSTimer 是可行的方法,对每周更新执行类似的操作:
新的一周开始时将触发以下函数:
对时钟使用类似的函数:
每秒触发此函数:
There is no need to use threads for simple tasks like these.
NSTimer is the way to go, do something like this for the weekly updates:
The following function will be triggered when the new week starts:
Use something like this for the clock:
Which triggers this function every second: