Ios 填充了时间[hh:mm:ss]的数组数组
如何创建格式为[hh:mm:ss]的时间数组 数组数组将包括一周中的每一天,然后是一周中每一天的特定时间 当最后我要比较当前时间[hh:mm:ss]与数组中的时间[hh:mm:ss]以找出下一个最近时间之间的时差时,最好的解决方案是什么Ios 填充了时间[hh:mm:ss]的数组数组,ios,objective-c,arrays,timer,Ios,Objective C,Arrays,Timer,如何创建格式为[hh:mm:ss]的时间数组 数组数组将包括一周中的每一天,然后是一周中每一天的特定时间 当最后我要比较当前时间[hh:mm:ss]与数组中的时间[hh:mm:ss]以找出下一个最近时间之间的时差时,最好的解决方案是什么 谢谢 下面是一个示例,说明如何使用包含时间(从技术上讲是日期,其中包含时间成分)的NSArray字典。稍后我将演示如何在比较时间时忽略日期部分 基本上,您将为一周中的每一天构建一个NSArray,其中包含您希望在该天使用的所有时间。然后,您将这7个数组中的每一个
谢谢 下面是一个示例,说明如何使用包含时间(从技术上讲是日期,其中包含时间成分)的NSArray字典。稍后我将演示如何在比较时间时忽略日期部分 基本上,您将为一周中的每一天构建一个NSArray,其中包含您希望在该天使用的所有时间。然后,您将这7个数组中的每一个都添加到NSDictionary中,并为一周中的某一天添加一个键(如果只添加了几天,但它会让您了解:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
// Here are just some dummy times. The date parts will be ignored in the compare function below.
NSDate *firstTime = [dateFormat dateFromString:@"1970-01-01 07:00:00 +0100"];
NSDate *secondTime = [dateFormat dateFromString:@"1970-01-01 10:19:00 +0100"];
NSDate *thirdTime = [dateFormat dateFromString:@"1970-01-01 15:00:00 +0100"];
// Build our day of the weeek arrays of times.
NSArray *mondayTimes = @[firstTime, secondTime];
NSArray *tuesdayTimes = @[firstTime, thirdTime];
NSArray *wednesdayTimes = @[firstTime, secondTime, thirdTime];
NSDictionary *weekdayTimes = @{@"Monday": mondayTimes, @"Tuesday" : tuesdayTimes, @"Wednesday" : wednesdayTimes };
拥有NSDictionary后,可以通过简单的键查找从字典中检索时间数组,如下所示:
NSArray *tuesdayTimesFromDictionary = [weekdayTimes objectForKey:@"Tuesday"];
要将所有时间与当前时间进行比较,只需在NSDATE数组中循环,找到自现在以来时间间隔最短的时间:
NSDate *now = [NSDate date];
double minTimeSinceNow = MAXFLOAT;
NSDate *closestTime;
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss Z"];
for( NSDate *time in tuesdayTimesFromDictionary)
{
double timeDiff = [self timeDifferenceBetween:now andDate:time];
NSLog( [NSString stringWithFormat:@"Time compare between to now(%@) and %@ is %f seconds away", [timeFormat stringFromDate:now], [timeFormat stringFromDate:time], timeDiff]);
if( abs(timeDiff) < minTimeSinceNow)
{
minTimeSinceNow = abs(timeDiff);
closestTime = time;
}
}
NSLog( [NSString stringWithFormat:@"Closest time to now (%@) is: %@, which is %f seconds away", [timeFormat stringFromDate:now], [timeFormat stringFromDate:closestTime], minTimeSinceNow]);
我想你会想要一本NSArrays字典,字典中的每个条目都有一周中某一天的键。对于时间数组,我只需要使用NSDate,它可以让你轻松地计算数组中两个时间之间的差异。非常感谢!这真的很有帮助,我将在这个周末尝试使用它看看我想到了什么!
- (double)timeDifferenceBetween:(NSDate *)date1 andDate:(NSDate *)date2
{
unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components1 = [calendar components:flags fromDate:date1];
NSDateComponents* components2 = [calendar components:flags fromDate:date2];
NSDate* timeOnly1 = [calendar dateFromComponents:components1];
NSDate* timeOnly2 = [calendar dateFromComponents:components2];
return [timeOnly1 timeIntervalSince1970] - [timeOnly2 timeIntervalSince1970];
}