문제

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