为 getRequestedUpdateDateWithHandler 构建日期:

Constructing a date for getRequestedUpdateDateWithHandler:

我需要每天午夜更新我的 watchOS 复杂功能。

startOfDay 是一天的开始(即今天中午 12 点)。

我应该像这样在今天的开始加上一天吗?

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)
}

或者我不应该在代码中添加一天,而只是做这样的事情:

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)
}

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

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

第二个代码 return 今天的中午 12 点,这已经是过去了。