Can i get a notification, when a new day begins?

后端 未结 4 1874
Happy的楠姐
Happy的楠姐 2020-12-05 15:30

In Cocoa, is there a notification i can register for, that informs me, when a new day begins - at 00h:00min:01s in the morning?

4条回答
  •  天涯浪人
    2020-12-05 16:15

    There's no notification that I know of. You can get a timer to fire whenever a new day begins like so:

    [[NSTimer alloc] initWithFireDate:midnight
                             interval:60 * 60 * 24 //one day, in seconds
                               target:someObj
                             selector:@selector(someSelector)
                             userInfo:nil
                              repeats:YES];
    

    The trick is getting an NSDate set to midnight. Check the Date and Time Programming Guide for how to do that with date components and the like.

    EDIT: see this question for how to get the midnight NSDate.

提交回复
热议问题