了解 NSCalendar 单位(请像我五岁一样解释一下)

发布于 2024-12-04 15:20:43 字数 1593 浏览 0 评论 0原文

我想弄清楚如何在特定日期触发本地通知。

我必须使用日历单位,但我发现的关于它们的解释是一个糟糕的作品。你们能向我解释一下,就像我是五岁一样,它们代表什么间隔吗?

我把我自己认为知道的都填了,如有错误请指正,其他的也都补上。

NSEraCalendarUnit = ????? era? 
NSYearCalendarUnit = event will repeat once in a year at the same month, day and hour
NSMonthCalendarUnit = event will repeat once a month at the same day and hour
NSDayCalendarUnit = event will repeat every day at the same hour
NSHourCalendarUnit = event will repeat every hour at the same minute
NSMinuteCalendarUnit = event will repeat every minute at the same second
NSSecondCalendarUnit = event will repeat every second
NSWeekCalendarUnit = event will repeat the same day of week every week 
NSWeekdayCalendarUnit = ?????? can I make it repeat just specific days? I mean, monday to friday but not saturday and sunday?
NSWeekdayOrdinalCalendarUnit = ????? how do I use that?
NSQuarterCalendarUnit = when exactly will it repeat? every 3 months?
NSCalendarCalendarUnit = ?????????
NSTimeZoneCalendarUnit = ????????

这是我需要做的一个例子:

UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
    alarm.fireDate = aDate;
    alarm.timeZone = [NSTimeZone systemTimeZone];
    alarm.repeatInterval = WHAT DO I PUT HERE? TO MAKE IT REPEAT JUST SATURDAYS AND SUNDAYS?;
    alarm.soundName = @"alarm.caf";
    alarm.alertBody = @"hi there";
    [app scheduleLocalNotification:alarm];
}

我需要以以下可能性之一对本地通知进行编程:

  • 从周一到周五每天触发,周六和周日除外,
  • 每个周六和周日触发,没有其他一天
  • 重复一周中的同一天,

我应该使用重复间隔的什么值来实现此目的?

谢谢

I am trying to figure out how to fire a local notification on specific days.

I have to use the calendar units, but the explanations I found about them are a stinking piece. Can you guys please explain to me, as if I were five, what intervals they represent?

I have filled the ones I think I know, please correct me if I am wrong and fill the others.

NSEraCalendarUnit = ????? era? 
NSYearCalendarUnit = event will repeat once in a year at the same month, day and hour
NSMonthCalendarUnit = event will repeat once a month at the same day and hour
NSDayCalendarUnit = event will repeat every day at the same hour
NSHourCalendarUnit = event will repeat every hour at the same minute
NSMinuteCalendarUnit = event will repeat every minute at the same second
NSSecondCalendarUnit = event will repeat every second
NSWeekCalendarUnit = event will repeat the same day of week every week 
NSWeekdayCalendarUnit = ?????? can I make it repeat just specific days? I mean, monday to friday but not saturday and sunday?
NSWeekdayOrdinalCalendarUnit = ????? how do I use that?
NSQuarterCalendarUnit = when exactly will it repeat? every 3 months?
NSCalendarCalendarUnit = ?????????
NSTimeZoneCalendarUnit = ????????

this is an example of what I need to do:

UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
    alarm.fireDate = aDate;
    alarm.timeZone = [NSTimeZone systemTimeZone];
    alarm.repeatInterval = WHAT DO I PUT HERE? TO MAKE IT REPEAT JUST SATURDAYS AND SUNDAYS?;
    alarm.soundName = @"alarm.caf";
    alarm.alertBody = @"hi there";
    [app scheduleLocalNotification:alarm];
}

I need to program the local notifications in one of these possibilities:

  • to fire every day from monday to friday, except saturday and sunday
  • to fire every saturday and sunday and no other day
  • to repeat every week at the same day of week

what values should I use for the repeatInterval to accomplish this?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2024-12-11 15:20:44

我相信你想要的东西是做不到的。 这个问题可能会给出更多细节。

当然,您可以做的是制作两个单独的本地通知,都设置为每周重复,一个在星期六触发,一个在星期日触发。

I believe what you want can't be done. This question may give more details.

Of course, what you can do is make two separate local notifications, both set to repeat weekly, one that fires on Saturdays and one that fires on Sundays.

很酷又爱笑 2024-12-11 15:20:44

问:从周一到周五每天触发,周六和周日除外

A:只需获取五个 UILocalNotification 实例并设置周一到周五的三个触发日期(只要工作日是您想要的,触发日期的年、月、日是免费的)然后设置三个repeatInterval NSWeekdayCalendarUnit。之后将它们安排到UIApplication。

(ps:其他问题也可以用同样的方法解决)希望能成功!

Q:to fire every day from monday to friday, except saturday and sunday

A:just get five UILocalNotification instances and set thire firedate from Monday to Friday(the firedate's year ,month,day is free so long as the weekday is what you want).then set thire repeatInterval NSWeekdayCalendarUnit.after schedule them to UIApplication.

(ps:you can solve other questions use the same method) hope it work!

原野 2024-12-11 15:20:44

我相信你的做法是错误的。

这是我要对一个 5 岁的孩子说的话(尽管如果他 5 岁时就能理解 obj/c,我会很快雇用他)。

你想实现什么目标?您想发出警报还是想在一周中的特定日期运行特定操作?

他可能会说:“我不知道”,但在这里你实际上想在周六和周日运行特定的操作。

为此,您确实需要一个计时器,例如每天、每小时或每分钟运行,这并不重要,计时器只会触发检查该操作是否会发生的操作。

NSTimer* aTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] 
                                                   interval:3600*24 // every 24 hours for example 
                                                     target:self 
                                                   selector:@selector(onTimerTriggered:) 
                                                   userInfo:nil 
                                                    repeats:YES];

现在您需要实际检查一下我们是星期几。最重要的是,这是单独完成的。

- (void)onTimerTriggered:(id)sender
{
    // Create your calendar (gregorian is what you need)
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Create your components specified to only keep track of the week day of today
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

    // now you know which day we are !! (careful in the US, sunday is first day of the week)
    int weekday = [weekdayComponents weekday];

    // trigger alarm if needed
    if (weekday == 3 || weekday == 5) // wednesday or saturday
    { ... } // trigger your alarm instantly or wait until a specific hour it's up to you
}

总而言之,将你的问题分解为更具体的行动。您想在不同的日子创建特定的操作吗?

  1. 检查今天是星期几
  2. 行动!

NS--CalendarUnit 仅用于将特定日期分解为 Apple 所谓的 NSDateComponents,但这绝对是 Apple 处理日期分解的方式 并且不是用钢写成的其他语言。

Your are doing it the wrong way I believe.

Here is what I would say to a five year old (though if he can comprehend obj/c at 5 I'd hire him quickly).

What do you want to accomplish ? You want to fire an alarm or you want to run specific actions at specific days of the week ?

He would probably say: "i don't know" but here you actually want to run specific actions on saturday and sundays.

To do so, you indeed need a timer that will run for example every day, or every hour, or every minute it really doesn't matter, the timer will only trigger the action that checks whether or not the action will occur.

NSTimer* aTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] 
                                                   interval:3600*24 // every 24 hours for example 
                                                     target:self 
                                                   selector:@selector(onTimerTriggered:) 
                                                   userInfo:nil 
                                                    repeats:YES];

Now you need to actually check to see if which day of the week we are. The most important thing is that this is done separately.

- (void)onTimerTriggered:(id)sender
{
    // Create your calendar (gregorian is what you need)
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Create your components specified to only keep track of the week day of today
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

    // now you know which day we are !! (careful in the US, sunday is first day of the week)
    int weekday = [weekdayComponents weekday];

    // trigger alarm if needed
    if (weekday == 3 || weekday == 5) // wednesday or saturday
    { ... } // trigger your alarm instantly or wait until a specific hour it's up to you
}

So to sum up, decompose your problem into more specific actions. You want to action create specific actions on different days ?

  1. Check what day of the week it is
  2. Action !

The NS--CalendarUnit are only used to decompose a specific date into what Apple call a NSDateComponents but this is definitely Apple way of dealing with date decomposition and is not written in steel for other languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文