swift - 为 getRequestedUpdateDateWithHandler 构造日期 :

标签 swift watchos 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)
}

最佳答案

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

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

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

关于swift - 为 getRequestedUpdateDateWithHandler 构造日期 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36877465/

相关文章:

ios - 如何在 CloudKit 记录中搜索部分单词?

swift - 点的坐标 - ios-charts [Swift]

ios - 重建模型时 Xcode 提示将 swift 与 objective c 桥接

xcode - WatchKit 2.0 应用程序的包 ID person.bundle.Ropes.watchkitapp 没有以父应用程序的包 ID 为前缀,后跟 '.' ;预期的前缀

ios - NSUbiquitousKeyValueStore 不可用 - watchOS 2

xcode - 有没有办法让 Apple Watch 每天在后台运行一次代码?

watchkit - iOS WatchOS - WatchKit 应用程序和 WatchKit 应用程序扩展之间有什么区别?

ios - 如何使用 'SWRevealViewController' 在 UITabBarItem 而不是 UIButton 中设置 RevealToggle 方法?

ios - 为什么我的 Apple Watch OS 应用程序只能在我的 iOS 应用程序处于事件状态时才能收到来 self 的 iOS 应用程序的消息?

ios - Apple Watch 上的并发症不显示我的信息