检查日期何时过去 - Swift [英] Checking when a date has passed - Swift

查看:63
本文介绍了检查日期何时过去 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此外,当应用程序终止或在后台或其他任何事情时,我真的不需要知道这些信息.我只需要知道当应用程序运行并且用户实际与之交互时日期是否已更改.

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

解决方案

实施以观察

NSCalendarDayChangedNotification

<块引用>

在系统日历日发生变化时发布,具体取决于通过系统日历、语言环境和时区.此通知确实不提供对象.

如果在日期变化时设备处于睡眠状态,则此通知将在唤醒时发布.只会发布一个通知如果设备已休眠多天,则唤醒.

不保证此通知的及时性观察员会收到.因此,你不应该依赖这个随时发布或接收通知.

通知通过[NSNotificationCenter defaultCenter]发布.

示例:

applicationDidFinishLaunching中添加

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

并实现方法

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

或使用块 API.

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

Well, the title pretty much says it all. What I am trying to do is check when a date has passed. So, for example let us say that a user is using my app and then they go to bed and check my app in the morning. When my app opens up I need to check if the day has changed at all.

Also I don't really need to know this information when the app is terminated or in the background or anything. I just need to know if the date has changed when the app is running and the user is actually interacting with it.

Note: I have looked at other Stack Overflow Posts pertaining to this issue but none of them have helped me.

解决方案

Implement to observe

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].

Example:

In applicationDidFinishLaunching add

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

and implement the method

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

or use the block API.

If the class including the observer is not the application delegate class you might remove the observer at some time.

这篇关于检查日期何时过去 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆