Search code examples
iosnsdatecomponentsnstimeinterval

NSTimeInterval vs NSDateComponents for generic time entry view controller


i am building a SetTimeViewController object to generically let a user select a time of day. i am trying to decide whether i should use an NSDateComponents object or an NSTimeInterval to represent the time of day, i.e.

@property (weak, nonatomic) NSDateComponents *time;

vs.

@property (assign) NSTimeInterval time;

i am not experienced enough to know which of these approaches is superior, nor why. any advice would be much appreciated.


Solution

  • NSDateComponents would seem to fit your description better because it would allow you to specify the hour, minute and second individually without any consideration of the date.

    Technically NSTimeInterval could do that for you too if you chose only to store the number of seconds since midnight to the time you want.

    Which is best depends very much on what you're going to use the time for. Both could be applied to an NSDate instance, but NSDateComponents is most likely better because you could set the other date components and then use dateFromComponents:.