正在听更改为本周吗?

发布于 2024-10-31 20:17:23 字数 460 浏览 3 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(1

败给现实 2024-11-07 20:17:23

不需要使用线程来执行这些简单的任务。
NSTimer 是可行的方法,对每周更新执行类似的操作:

// Exact moment the new week starts
NSDate *date = 

// Imagine the new week starts in 5 seconds
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5]; 

NSTimer *timer = [[NSTimer alloc]
                  initWithFireDate: date
                  interval: 60*60*24*7 // Equals one week
                  target: self
                  selector: @selector(updateWeekLabel:)
                  userInfo: nil
                  repeats: NO
                  ];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

新的一周开始时将触发以下函数:

-(void)updateWeekLabel:(NSTimer*)theTimer
{
    // Get current week and update the label.
    // Wait for next week or invalidate the timer like this:
    [theTimer invalidate];
}

对时钟使用类似的函数:

NSTimer *timer = [NSTimer 
                  scheduledTimerWithTimeInterval: 1 
                  target: self 
                  selector: @selector(updateTimeLabel:) 
                  userInfo: nil 
                  repeats: YES
                  ];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

每秒触发此函数:

-(void)updateTimeLabel:(NSTimer*)theTimer
{
    // Get current time and update the label.
}

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:

// Exact moment the new week starts
NSDate *date = 

// Imagine the new week starts in 5 seconds
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5]; 

NSTimer *timer = [[NSTimer alloc]
                  initWithFireDate: date
                  interval: 60*60*24*7 // Equals one week
                  target: self
                  selector: @selector(updateWeekLabel:)
                  userInfo: nil
                  repeats: NO
                  ];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

The following function will be triggered when the new week starts:

-(void)updateWeekLabel:(NSTimer*)theTimer
{
    // Get current week and update the label.
    // Wait for next week or invalidate the timer like this:
    [theTimer invalidate];
}

Use something like this for the clock:

NSTimer *timer = [NSTimer 
                  scheduledTimerWithTimeInterval: 1 
                  target: self 
                  selector: @selector(updateTimeLabel:) 
                  userInfo: nil 
                  repeats: YES
                  ];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

Which triggers this function every second:

-(void)updateTimeLabel:(NSTimer*)theTimer
{
    // Get current time and update the label.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文