首先,你为什么要添加NSNumbers
到你的NSArray
?这些应该是NSDate
对象。
如果你这样做,你可以像这样使用 NSDate 的比较方法:
[date1 compare: date2];
在您的情况下,如果您想在当前时间之后立即显示时间,我会这样做:
NSDate *currentTime = [NSDate dateWithTimeIntervalSinceNow:0];
NSUInteger smallestInterval = -1;
NSInteger smallestDateIndex = -1;
for(int i = 0; i < array.count; i++){
if([currentTime timeIntervalSinceDate:[array objectAtIndex:i]] <= 0){
continue;
}
if(smallestDateIndex == -1){
smallestDateIndex = i;
smallestInterval = [currentTime timeIntervalSinceDate:[array objectAtIndex:i]];
} else if([currentTime timeIntervalSinceDate:[array objectAtIndex:i]] < smallestInterval){
smallestInterval = [currentTime timeIntervalSinceDate:[array objectAtIndex:i]];
smallestDateIndex = i;
}
}
if(smallestDateIndex > -1){
NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:@"HH:mm"];
myLabel.text = [formatter stringFromDate:[array objectAtIndex:smallestDateIndex]];
}