如何使用 NSDate 制作计数器?

发布于 2024-10-16 01:57:39 字数 109 浏览 3 评论 0原文

我想开发一个餐厅系统,它想要计算订单剩余时间(比如披萨订单剩余 5 薄荷糖),

我想知道这个计数器的基本代码是什么,比如我如何使用 NSDate 或任何其他类???

请指导

I want to develop a system for restaurant, which want to calculate time remaining in order(like 5 mints remaining in order of pizza),

I want to know what basic code be for this counter, like how can I use NSDate or any other class???

Please guide

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

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

发布评论

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

评论(2

浮光之海 2024-10-23 01:57:39

获取自某个 NSDate 以来的秒数:

[[NSDate date] timeIntervalSinceDate:startingDate];

[NSDate date] 返回当前 NSDate 对象,并且等于当前日期和时间。

Get amount of seconds since a certain NSDate:

[[NSDate date] timeIntervalSinceDate:startingDate];

[NSDate date] returns the current NSDate object, and thats equal to the current date and time.

请叫√我孤独 2024-10-23 01:57:39

我认为您可以实现一个循环计时器,每 30 秒一次(不要太快),它将轮询所有待处理的请求并更新每个订单的倒计时,并向您发出所有达到倒计时的订单的信号。

例如:假设您点了 5 分钟的订单。然后,您将通过在当前日期上添加 5*60 秒来计算订单准备就绪的日期:

NSDate *orderReadyAt = [NSDate dateWithTimeIntervalSinceNow:5*60];

然后,您将该日期存储到订单结束日期属性中。例如:


Order *myOrder = [[Order alloc] init];
... make your order here ...
myOrder.readyDate = orderReadyAt;

在应用程序启动时,您将定义一个循环计时器,该计时器每 30 秒触发一次,并调用“countdownCheck”函数:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(countdownCheck) userInfo:nil repeats:YES];

此时,您在 countdownCheck 中要做的就是循环遍历您的 Order 实例,并为每个实例计算剩余时间:


-(void)countdownCheck {
for(Order *aOrder in allOrders) {
  NSTimeInterval secondsRemaining = [aOrder.readyDate timeIntervalSinceNow];
  //... do something: update your GUI, show an alert if secondsRemaining<0...
}
}

显然,这种方法对时间要求不高,唯一的要求是在 30 秒限制内运行 countdownCheck 内的代码,这当然绰绰有余。理想情况下,只要循环足够快,您就可以将其设置为 1 秒。

I think you can implement a recurring timer, paced each 30 seconds (don't do it too fast), that will poll all pending requests and update the countdown for each order and will signal you all orders that reached the countdown.

E.g.: let's say you take a 5 minutes order. Then you will calculate the date the order will be ready by adding 5*60 seconds to the current date:

NSDate *orderReadyAt = [NSDate dateWithTimeIntervalSinceNow:5*60];

Then you will store this date to your order end date property. E.g.:


Order *myOrder = [[Order alloc] init];
... make your order here ...
myOrder.readyDate = orderReadyAt;

At application startup you will define a recurring timer that will trigger each 30 seconds and will call the "countdownCheck" function:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(countdownCheck) userInfo:nil repeats:YES];

At this point what you have to do in your countdownCheck is to loop through your Order instances and for each of them calculate the time remaining:


-(void)countdownCheck {
for(Order *aOrder in allOrders) {
  NSTimeInterval secondsRemaining = [aOrder.readyDate timeIntervalSinceNow];
  //... do something: update your GUI, show an alert if secondsRemaining<0...
}
}

Clearly this approach is not time critical, the only requirement you have is to run the code inside the countdownCheck in the 30 seconds limit, which is of course more than enough. You can ideally set it to 1 second only, provided your loop is fast enough.

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