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).
This is what I ended up doing, using
NSDateandNSDateFormatteronly, where open is anNSStringformatted in military time.The takeaway lesson is that it’s perfectly okay to have
NSDateobjects where you only set the hourly components- everything else is only set to January 1, 1970, and it doesn’t cause any issues.