我正在使用以下代码尝试使用当前系统时间告诉我下一次在数组中。它注销的全部是数组中的最后一个数字。我已经搜索过了,这就是我最终走到哪里的原因,但却永远无法让它正常工作。感谢
self.array = @[@"00:15", @"01:35, "@"05:30", @"06:10", @"07:05", @"07:55", @"08:45", @"09:35", @"10:40", @"11:25", @"12:20", @"13:10", @"14:05", @"15:00", @"15:45", @"16:40", @"17:30", @"18:20", @"19:20", @"20:10", @"21:00", @"22:05", @"22:55"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
for(NSString *stringTime in self.array) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
if([formattedCurrentDate earlierDate:dateFromString]) {
self.nextTime.text = stringTime;
}
}
答案 0 :(得分:1)
您的代码行为完全正常,您返回的时间早于当前时间,这意味着每次都分配给self.nextTime.text,但您只看到最后一个分配,即数组中的最后一项。
如果正确执行, if 语句总是返回true,因此除非提供的对象不是NSDate,否则总是会返回true。
以下是获取 所需内容的正确方法,而无需担心订购数组。
NSArray *array = @[@"16:40", @"05:30", @"06:10", @"07:05", @"07:55", @"08:45", @"09:35", @"10:40", @"11:25", @"12:20", @"13:10", @"14:05", @"15:00", @"15:45", @"16:40", @"17:30", @"18:20", @"19:20", @"20:10", @"21:00", @"22:05", @"22:55", @"23:55", @"01:35"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
NSDate *laterDate;
NSDate *closestDate;
for(NSString *stringTime in array) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
laterDate = [formattedCurrentDate laterDate:dateFromString];
if(![laterDate isEqualToDate:formattedCurrentDate]){
closestDate = [laterDate earlierDate:closestDate];
}
}
NSLog(@"Closest Date: %@", [dateFormat stringFromDate:closestDate] );
答案 1 :(得分:0)
如果您想要一个更通用的解决方案,而不依赖于数组的顺序,您可以使用以下代码:
NSArray *array = @[@"02:25", @"01:35", @"05:30", @"06:10", @"07:05", @"07:55", @"08:45", @"09:35", @"10:40", @"11:25", @"12:20", @"13:10", @"14:05", @"15:00", @"15:45", @"16:40", @"17:30", @"18:20", @"19:20", @"20:10", @"21:00", @"22:05", @"22:55", @"23:55"];
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
NSTimeInterval currentInterval = [formattedCurrentDate timeIntervalSince1970];
NSTimeInterval smallerInterval = CGFLOAT_MAX;
for(NSString *stringTime in array) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
NSTimeInterval dateFromStringInterval = [dateFromString timeIntervalSince1970];
NSTimeInterval actualInterval = (dateFromStringInterval - currentInterval);
if(actualInterval > 0 && actualInterval < smallerInterval){
smallerInterval = actualInterval;
self.nextTime.text = stringTime;
}
}
此代码获取当前小时最接近的小时,与其在数组中的索引无关。并且不需要更多的分配或任何数组排序,例如@Sandeep解决方案(确实有效)。
解释: actualInterval获取所有日期和当前日期之间的秒数差异。这个变量(实际上是双倍的)对未来的小时数只会是正数(过去几小时会是负数)。
因此,所有代码正在做的是在未来几小时之间获得较小的差异。我认为这是本案的最佳解决方案。
希望这会有所帮助。
答案 2 :(得分:0)
NSArray *array = @[@"23:15", @"01:35", @"05:30", @"06:10", @"07:05", @"07:55", @"08:45", @"09:35", @"10:40", @"11:25", @"12:20", @"13:10", @"14:05", @"15:00", @"15:45", @"16:40", @"17:30", @"18:20", @"19:20", @"20:10", @"21:00", @"22:05", @"22:55", @"23:55"];
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
//Convert all strings to dates
for (NSString *str in array)
{
NSDate *formattedCurrentDate = [dateFormat dateFromString: str];
[datesArray addObject:formattedCurrentDate];
}
//Sort dates.
NSArray *sortedArray = [datesArray sortedArrayUsingSelector:@selector(compare:)];
NSString *date = [dateFormat stringFromDate:[NSDate date]];
NSDate *formattedTodatDate = [dateFormat dateFromString:date];
//Print the desired date
for(NSDate *stringTime in sortedArray)
{
if([formattedTodatDate compare:stringTime] == NSOrderedAscending)
{
NSLog(@"Result is %@", [dateFormat stringFromDate:stringTime]);
break;
}
}
试试上面的代码。我只是按照其他人提到的排序日期。
答案 3 :(得分:-1)
如果你的功能正常。 改变:
for(NSString *stringTime in self.array) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
if([formattedCurrentDate earlierDate:dateFromString]) {
self.nextTime.text = stringTime;
}
}
为:
for(NSString *stringTime in self.array) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
if([formattedCurrentDate compare:dateFromString] == NSOrderedAscending) {
self.nextTime.text = stringTime;
break;
}
}