Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top