gpt4 book ai didi

swift - 检查日期何时过去 - Swift

转载 作者:可可西里 更新时间:2023-11-01 00:36:55 25 4
gpt4 key购买 nike

好吧,标题几乎说明了一切。我想做的是检查日期何时过去。因此,例如,假设用户正在使用我的应用程序,然后他们在早上上床 sleep 并检查我的应用程序。当我的应用程序打开时,我需要检查日期是否发生了变化。

此外,当应用程序终止或在后台运行时,我真的不需要知道这些信息。我只需要知道当应用程序运行时日期是否已更改以及用户是否实际与其交互。

注意:我查看了与此问题相关的其他 Stack Overflow 帖子,但没有一个对我有帮助。

最佳答案

实现观察

NSCalendarDayChangedNotification

Posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone. This notification does not provide an object.

If the the device is asleep when the day changes, this notification will be posted on wakeup. Only one notification will be posted on wakeup if the device has been asleep for multiple days.

There are no guarantees about the timeliness of when this notification will be received by observers. As such, you should not rely on this notification being posted or received at any precise time.

The notification is posted through [NSNotificationCenter defaultCenter].

例子:

applicationDidFinishLaunching中添加

NSNotificationCenter.defaultCenter().addObserver(self, selector:"calendarDayDidChange:", name:NSCalendarDayChangedNotification, object:nil)

并实现方法

func calendarDayDidChange(notification : NSNotification)
{
doSomethingWhenDayHasChanged()
}

或使用 block API。

如果包含观察者的类不是应用程序委托(delegate)类,您可能会在某个时候删除观察者。

关于swift - 检查日期何时过去 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com