Objective c 在一天中的某个时间通知

Objective c 在一天中的某个时间通知,objective-c,macos,nstimer,nsnotificationcenter,Objective C,Macos,Nstimer,Nsnotificationcenter,我需要我的应用程序在一天中的某个时间运行该方法,我认为最好的方法是设置一个通知,通知它何时到达该时间。我以前使用过NSNotificationCenter,但我不知道如何在一天中的某个时间设置它 编辑: 我将使用NSTimer以另一种方式处理它。这是我的密码 NSCalendar* myCalendar = [NSCalendar currentCalendar]; NSDateComponents* components = [myCalendar components:NSYearC

我需要我的应用程序在一天中的某个时间运行该方法,我认为最好的方法是设置一个通知,通知它何时到达该时间。我以前使用过NSNotificationCenter,但我不知道如何在一天中的某个时间设置它

编辑: 我将使用NSTimer以另一种方式处理它。这是我的密码

    NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                             fromDate:[NSDate date]];
[components setHour: 10];
[components setMinute: 5];
[components setSecond: 0];
NSDate *date = [myCalendar dateFromComponents:components];

NSTimer *t = [[NSTimer alloc] initWithFireDate:date
                                      interval:0.0
                                        target:self
                                      selector:@selector(fired:)
                                      userInfo:nil
                                       repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
使用类方法。创建计时器对象并相应地设置点火日期。在计时器回调方法中,实现发布所需通知的方法


另一种方法是使用。在selector方法中发布所需通知。

使用NSTimer可以设置点火时间吗?您可以使用initWithFireDate:interval:target:selector:userInfo:repeats Int方法。这将采用NSDate参数,您可以在其中指定一天中的什么时间启动计时器。”setFireDate:'方法可用于在稍后阶段更改计时器fireDate。好的,这很有意义,我如何告诉它一天中什么时候点火?好的,我想我知道了。谢谢