构造getRequestedUpdateDateWithHandler的日期:

时间:2016-04-26 23:16:28

标签: swift watch-os apple-watch-complication clockkit

我需要每天午夜更新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)
}

1 个答案:

答案 0 :(得分:1)

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

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

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