How can I create an array of certain times?

眉间皱痕 提交于 2019-12-25 20:38:13

问题


I want to create an array of times and than sort through them to find when the next closest time in the array is (if it passes a time, then it will choose the next one as next closest). How can I do this? I don't want it to specify year, month, or day. I just want to filter through the time of day (hours, minutes, seconds). I would like to get how many seconds until the next time in the NSArray. I have looked at NSDate and noticed there is a timeIntervalSinceDate method but I do not know how to create the NSDate object to compare it with.


回答1:


NSDate * date = [NSDate date];
NSArray * array = @[];
NSUInteger index =
[array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return ![[((NSDate *)obj) earlierDate:date] isEqualToDate:date];
}];
NSDate * refDate = nil;

if (index != NSNotFound)
    refDate = array[index];



回答2:


The other poster gave you a solution using NSDates. NSDates are objects which specify instants in time (including the year, month, day, hour, minute, second, and fraction of a second.)

If you want to work with times that only reflect hours/minutes/seconds, I suggest you just use integer math based on seconds/day:

NSUInteger totalSeconds = hours * 60 * 60 + minutes * 60 seconds;

You can then create an NSArray of NSNumber values that hold second counts, and manipulate them as desired.

You might write a method to convert hour/minute/second values into an NSNumber:

- (NSNumber *) numberWithHour: (NSUInteger) hour 
    minute: (NSUInteger) minute 
    second: (NSUInteger) second;
{
   return @(hour*60*60 + minute*60  second);
}

And then use that method to create an array of NSNumbers

NSMutableArray *timesArray = [NSMutableArray new];
[timesArray addObject: [self numberWithHour:  7 minute: 30 second:  0]];
[timesArray addObject: [self numberWithHour:  9 minute: 23 second: 17]];
[timesArray addObject: [self numberWithHour: 12 minute:  3 second: 52]];
[timesArray addObject: [self numberWithHour: 23 minute: 53 second: 59]];
};

To get the hours/minutes/ seconds for the current date, you'd use NSDate, NSCalendar, and NSDateComponents:

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];

NSDateComponents comps = 
  [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
    fromDate: now];
  int hour = [components hour];
  int minute = [components minute];
  int second = [components second];


unsigned long nowTotalSeconds = hours * 60 * 60 + minutes * 60 seconds;

And once you have calculated the total seconds value for today, you could loop through your array of time values and find the next future time using the NSArray method indexOfObjectPassingTest

NSUInteger futureTimeIndex = [timesArray indexOfObjectPassingTest: 
  ^BOOL(NSNumber *obj, NSUInteger idx, BOOL *stop)
{ 
   if (obj.unsignedIntegerValue > nowTotalSeconds)
     return idx;
}
if (futureTimeIndex != NSNotFound)
  NSInteger secondsUntilNextTime = 
    timesArray[futureTimeIndex].unsignedIntegerValue - nowTotalSeconds;


来源:https://stackoverflow.com/questions/23501899/how-can-i-create-an-array-of-certain-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!