0

I need to update my watchOS complication at midnight every day.

startOfDay is the beginning of the day (i.e., 12 AM today).

Should I add a day to the start of today like this?

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    let startOfDay = NSDate().startOfDay
    let components = NSDateComponents()
    components.day = 1
    let startOfNextDay = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: startOfDay, options: NSCalendarOptions())
    handler(startOfNextDay)
}

Or should I not add a day to the code, and just do something like this:

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    let startOfDay = NSDate().startOfDay
    handler(startOfDay)
}
4

1 回答 1

1

您希望将日期提前一天,因为您希望下一次请求的更新发生在明天的午夜。第一种方法可以满足您的要求,但您可以将其简化如下:

let calendar = NSCalendar.currentCalendar()
let startOfDay = calendar.startOfDayForDate(NSDate())
let startOfNextDay = calendar.dateByAddingUnit(.Day, value: 1, toDate: startOfDay, options: NSCalendarOptions())!

第二个代码将返回今天的上午 12 点,这已经是过去的时间了。

于 2016-04-26T23:58:02.613 回答