dave adelson
dave adelson

Reputation: 1013

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.

Upvotes: 2

Views: 309

Answers (2)

ryan cumley
ryan cumley

Reputation: 1931

NSDate is the class where you're probably going to find what you're looking for.

You might start playing around to get familiar like this:

NSDate* one = [[NSDate date];
NSDate* two = [[NSDate date];

NSTimeInterval* delta = [two timeIntervalSinceDate:one];

Set a breakpoint and step into the method and you can see what's going on.

Branch out into the other NSDate class methods from there, and you'll be up to speed in no time.

Upvotes: 0

Wain
Wain

Reputation: 119031

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:.

Upvotes: 1

Related Questions