如何从 UILocalNotification 对象获取下一个触发日期
我有一个 UILocalNotification 对象,我设置了重复间隔天、周和月。我在访问该对象的起火日期时完全没有任何问题:
[cell.detailTextLabel setText:[notification1.fireDate description]];
但我在获取下一个起火日期时遇到了麻烦。如果我将上面的 notification1 对象打印到控制台,我会得到以下信息:
<UIConcreteLocalNotification: 0x613e060>{fire date = 2010-11-29 03:53:52 GMT, time zone = America/Denver (MST) offset -25200, repeat interval = 16, next fire date = 2010-11-30 03:53:52 GMT}
该对象包含我需要显示下一个火灾日期的值或数据......但我找不到它!有人知道我可以在哪里以编程方式获取它吗?
谢谢
I have a UILocalNotification object that I have setup with repeat intervals day, week, and month. I am having no troubles at all accessing the fire date of the object:
[cell.detailTextLabel setText:[notification1.fireDate description]];
But I am having troubles getting the next fire date. If I print out the above notification1 object to the console, I get this:
<UIConcreteLocalNotification: 0x613e060>{fire date = 2010-11-29 03:53:52 GMT, time zone = America/Denver (MST) offset -25200, repeat interval = 16, next fire date = 2010-11-30 03:53:52 GMT}
This object contains somewhere the value or data I need to display the next fire date...but I can't find it! Does anybody know where I can get it programmatically?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要计算重复
UILocalNotification
的下一个触发日期,您必须:repeatInterval
时间(即它的fireDate
属性)和现在。fireDate
中。这是一种方法:
这在许多情况下都有效,但在一种情况下它不起作用:
假设:
repeatInterval
是NSDayCalendaryUnit
(即每天重复)上面的代码将计算差值到 7 天(01/01 + 7 天 = 08/01),将它们添加到
fireDate
,从而将nextFireDate
设置为 08/01 下午 2 点。但那已经是过去的事了,我们希望nextFireDate
为 09/01 下午 2 点!因此,如果使用上面的代码并且您的
repeatInterval
是NSDayCalendaryUnit
,则添加以下行:我将此答案标记为社区 wiki,如果您找到了更好的答案,请随时编辑它计算方法!
To calculate the next fire date for a repeating
UILocalNotification
, you have to:repeatInterval
there's been between the notification's original fire date (i.e. itsfireDate
property) and now.fireDate
.Here's one approach:
This works in many scenarios, but here's a scenario where it won't work:
Suppose that:
repeatInterval
isNSDayCalendaryUnit
(i.e. repeat daily)The above code will calculate the difference to 7 days (01/01 + 7 days = 08/01), add them to
fireDate
, and thus setnextFireDate
to 08/01 at 2pm. But that's in the past, we wantnextFireDate
to be 09/01 at 2pm!So if using the above code and your
repeatInterval
isNSDayCalendaryUnit
, then add these lines:I marked this answer as community wiki, feel free to edit it if you have found a better way to do the calculation!
我认为下一个触发日期不能作为属性使用,而是根据
fireDate
和repeatInterval
计算得出。对于不同的时区和其他令人讨厌的事情,日期计算可能会很棘手。在您的示例中,您选择了每日重复并计算下一次触发日期,您可以执行以下操作:如果您使用其他重复间隔,则必须相应地更改代码。如果您要使用
NSMonthCalendarUnit
,则必须使用components.month = 1
。I don't think the next fire date is available as a property but rather calculated from
fireDate
andrepeatInterval
. Date calculating can be tricky with different time zones and other nasty things. In your example you have chosen a daily repeat and to calculate the next fire date you can do something along the lines of:If you use some other repeat interval you would have to change the code accordingly. If you were to use
NSMonthCalendarUnit
you would have to usecomponents.month = 1
instead.我只需添加重复间隔,直到未来的日期:
i would just add the repeatInterval until the date lies in the future:
这是在 Swift 4 中并使用日历的
nextDate
函数。This is in Swift 4 and using calendar's
nextDate
func.