使用特定的时间和日期(不是日期)来激活“if” Xcode 中的函数

发布于 2025-01-07 02:10:26 字数 993 浏览 3 评论 0 原文

我目前正在为我正在制作的一个节目拼凑一个流媒体广播应用程序,到目前为止,一切都还好 - 一旦你按下“收听直播!”,流媒体就可以工作了。按钮,因为这是主要目标,我很高兴。然而,我现在正尝试变得更聪明,并设置一个屏幕显示来显示节目何时播出;它每周只打开两个小时,所以我认为通过应用程序中的屏幕显示来显示它的打开和关闭时间会很好。没什么奇特的...这是到目前为止我在屏幕上看到的内容:

1)从这里获取的计数时钟 - 编辑:http://www.youtube.com/watch?v=pNVWST15pyc – 这非常简单。不敢相信我没有意识到将 hh 更改为 HH 就变成了 24 小时……天哪,我太蠢了。

2)仅显示星期几,而不是日期或月份的标签,取自此处 - NSCalendar显示工作日 - 因为应用程序只需要知道是否是星期日(播出日)。编辑:现在可以正常工作了,因为我是一个没有正确链接它的白痴。 :(

然而,我真正想做的是让应用程序为图像视图设置两个设置 - 一个“播出”和“关闭播出”开关 - 这样当周日晚上 8 点到 10 点之间时,它会显示“播出”图像,当它是任何其他时间时,它显示“停播”图像,我很确定这是一个 if 语句,但我不知道从哪里开始尝试将时钟和工作日位结合起来。任何人都可以提出一些建议吗?

另外,由于这些时间是格林威治标准时间,我想将应用程序锁定为格林威治标准时间,无论用户在世界的​​哪个地方,所以我想我可以这样做。通过将“nil”位更改为“Europe/London”来使用时钟代码的 timeZone:nil 位,但这样做只会使整个事情以壮观的方式崩溃(我已经以各种形式尝试过,但没有成功再次,建议。非常感谢

您提出看似简单的问题 - 除了 Xcode 上的一些成人学习课程之外,我还是个新手。 :D

I'm current piecing together a streaming radio app for a show I work on and so far, it's okay - the streaming works once you push the 'Listen Live!' button and since that's the main aim, I'm happy. However, I'm trying to get clever now and set up an on-screen display that shows when the show is on the air; it's only on for two hours a week, so I thought it'd be nice to show when it's on and off via an on-screen display in the app. Nothing fancy... here's what I've got on screen so far:

1) A counting clock taken from here - EDIT: http://www.youtube.com/watch?v=pNVWST15pyc – that's really simple. Can't believe I didn't realise that changing the hh to HH makes it 24-hour... god, I'm so dumb.

2) A label to show just the day of the week, not the date or month, taken from here - NSCalendar to Display Weekday - as the app only needs to know whether it's Sunday (the on-air day) or not. EDIT: Made this work properly now because I'm an idiot who didn't link it up properly. :(

What I really want to do, however, is have the app set up two settings for an image view - an 'on air' and 'off air' switch - so that when it's between 8pm and 10pm on Sunday night, it shows the 'on air' image and when it's any other time, it shows the 'off air' one. I'm pretty sure that's an if statement but I'm not sure where to start trying to combine the clock and weekday bits to make that work. Can anyone make some suggestions please?

Also, since those times are GMT, I want to lock the app into GMT regardless of where in the world the user is so it relates directly to the show. I'm guessing I can do that using the timeZone:nil bit of the clock code by changing the 'nil' bit to 'Europe/London', but doing so just makes the whole thing crash out in spectacular fashion (I've tried it in various forms with no success). Again, suggestions would be massively appreciated.

Apologies for asking what may seem like simple questions - aside from some adult learning courses on Xcode, I'm a bit of a novice. :D

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

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

发布评论

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

评论(1

风柔一江水 2025-01-14 02:10:26

您应该使用 NSDateComponents 对象,它有一个 -weekday 实例方法(1 是公历的星期日)。有关如何开始的详细信息,请参阅 Apple 的 日期和时间编程指南。下面是与您想要执行的操作相关的代码示例:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                     initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

NSDateComponents 还允许您获取和设置(通过所谓的访问器方法)小时、分钟、秒和时区。

对于你上面描述的问题,我会写一个这样的方法:

- (BOOL)isShowOnAir {
    BOOL onAir = NO;

    NSDate *now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:(NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
    [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSInteger day = [components day]; // Sunday == 1
    if (day == 1) {
        NSInteger hour = [components hour];
        //NSInteger minute = [components minute]; -- not used but here's how to access it
        if (hour == 20 || hour == 21) { // covers 8:00 - 9:59 PM
            onAir = YES;
        }
    }
    return onAir;
}

You should use the NSDateComponents object, which has a -weekday instance method (1 being Sunday for the gregorian calendar). Details on how to get started are in Apple's Date and Time Programming Guide. Here's a code sample that relates to what you are trying to do:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                     initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

NSDateComponents will also let you get and set (through so-called accessor methods) hour, minute, second, and timezone.

For the problem you described above, I would write a method like this:

- (BOOL)isShowOnAir {
    BOOL onAir = NO;

    NSDate *now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:(NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
    [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSInteger day = [components day]; // Sunday == 1
    if (day == 1) {
        NSInteger hour = [components hour];
        //NSInteger minute = [components minute]; -- not used but here's how to access it
        if (hour == 20 || hour == 21) { // covers 8:00 - 9:59 PM
            onAir = YES;
        }
    }
    return onAir;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文