在 Objective-C 中表示每小时范围

发布于 2024-11-01 11:15:51 字数 626 浏览 1 评论 0原文

我正在尝试创建一个类来表示 Obj-C 程序中的运行时间。我一直在考虑使用 NSDatesComponent 来做到这一点,但我觉得应该有一种更简单的方法。有人对如何执行此操作有任何提示吗?

在核心数据中,我有一个 shop 实体,它有一个 ivar,它是一个数组。该数组对于一周中的每一天都有七个条目,每天的值将是一个表示小时范围的 hoursOp 对象。 9-5、10-6 等。问题是,我不确定如何创建 hoursOp 类来表示一系列小时。我正在考虑使用 NSDateComponents,但不确定是否应该打扰。目前,我认为一个 hoursOp 对象应该包含两个变量:开始时间和结束时间。

此外,由于数组条目中可能需要多个 hoursOp 对象,情况变得更加复杂 - 如果 shop 的营业时间为 10-4,然后是 6-8,该怎么办?因此,它不是一个保存 hoursOp 对象的数组,而是一个二维数组,其中每个条目都是一个包含 1 个或多个 hoursOp 对象的数组。这有点复杂,但我相信一旦我弄清楚如何表示时间(或范围本身,如果有人有更好的建议),我就能让它工作。

I'm trying to create a class that represents hours of operation in an Obj-C program. I have been considering using NSDatesComponent to do this, but I feel there should be an easier way. Does anyone have any tips as to how to do this?

In Core Data, I would have a shop entity that would have an ivar that is an array. This array would have seven entries for each day of the week, and the value for each day would be an hoursOp object that would represent a range of hours. 9-5, 10-6, etc. The issue is, I'm not sure how to create my hoursOp class to represent a range of hours. I was thinking of using NSDateComponents, but am unsure if I should bother. Currently, I think that an hoursOp object should hold two variables: a starting time and an ending time.

Furthermore, this is complicated by the possible need for multiple hoursOp objects in an array entry- what if a shop has operating hours of 10-4, then 6-8? So instead of an array that holds hoursOp object, it would be a two-dimensional array where each entry is an array of its own holding 1 or more hoursOp objects. This is all a bit convoluted, but I believe I could get it to work, once I figure out how to represent the hours (or the ranges themselves, if anyone has a better suggestion).

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

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

发布评论

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

评论(2

我的黑色迷你裙 2024-11-08 11:15:51

很难说实现这一点的“最佳”方法,因为这取决于您对数据的处理方式,但如果您在 hoursOp 对象,您可以有一个 hoursOp 对象的一维数组,表示商店开门的时间间隔。

您将失去数组索引和星期几之间的一对一关系,这可能会使某些任务变得更加困难(例如,如果您不断查询该对象的周六商店营业时间,您将大量扫描整个数组),但否则您将走上拥有数组数组的道路。

It's hard to say the "best" way to implement this, because it depends on what you're doing with the data, but if you stored the day of the week, opening time and closing time inside your hoursOp object, you could have a one-dimensional array of hoursOp objects that represented time intervals when the store was open.

You'd lose the one-to-one relationship between your array index and the day of the week, which might make some tasks harder (if you're constantly querying this object for its Saturday store hours, for instance, you'll be scanning the entire array a lot), but otherwise you're going down the path of having an array of arrays.

不一样的天空 2024-11-08 11:15:51

这就是我最终所做的,仅使用 NSDateNSDateFormatter,其中 open 是在军事时间格式化的 NSString

-(void) setOpeningTimeWithString: (NSString *)open
{
    NSDateFormatter * formatFromInput = [[NSDateFormatter alloc] init];
    [formatFromInput setDateFormat:@"HH:mm"];
    self.openingTime = [formatFromInput dateFromString:open];   
    [formatFromInput release];  
}

重要的教训是,使用 NSDate 对象来仅设置每小时组件是完全可以的 - 其他所有内容都仅设置为 1970 年 1 月 1 日,并且不会导致任何问题。

This is what I ended up doing, using NSDate and NSDateFormatter only, where open is an NSString formatted in military time.

-(void) setOpeningTimeWithString: (NSString *)open
{
    NSDateFormatter * formatFromInput = [[NSDateFormatter alloc] init];
    [formatFromInput setDateFormat:@"HH:mm"];
    self.openingTime = [formatFromInput dateFromString:open];   
    [formatFromInput release];  
}

The takeaway lesson is that it's perfectly okay to have NSDate objects where you only set the hourly components- everything else is only set to January 1, 1970, and it doesn't cause any issues.

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