如何创建特定时间的数组? [英] How can I create an array of certain times?

查看:72
本文介绍了如何创建特定时间的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个时间数组,然后对它们进行排序以查找数组中下一个最近的时间是什么时候(如果它过了一段时间,那么它会选择下一个作为下一个最近的时间).我怎样才能做到这一点?我不希望它指定年、月或日.我只想过滤一天中的时间(小时、分钟、秒).我想在 NSArray 中获得下一次之前的秒数.我查看了 NSDate 并注意到有一个 timeIntervalSinceDate 方法,但我不知道如何创建 NSDate 对象来与它进行比较.

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.

推荐答案

另一张海报为您提供了使用 NSDates 的解决方案.NSDates 是指定时间瞬间的对象(包括年、月、日、小时、分钟、秒和秒的分数.)

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;

然后,您可以创建一个 NSNumber 值的 NSArray 来保存秒数,并根据需要对其进行操作.

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

您可以编写一个方法将小时/分钟/秒值转换为 NSNumber:

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);
}

然后使用该方法创建一个 NSNumbers 数组

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]];
};

要获取当前日期的小时/分钟/秒,您可以使用 NSDate、NSCalendar 和 NSDateComponents:

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;

一旦你计算了今天的总秒数,你就可以遍历你的时间值数组并使用 NSArray 方法 indexOfObjectPassingTest 找到下一个未来时间

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;

这篇关于如何创建特定时间的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆