-2

我有 NSMutableArray 和 NSDates 一些日期来自同一天。获取特定日期的 NSDates 数量的最佳方法是什么?

编辑:

对不起,我没有完全解释。我需要某种逻辑来从源数组中获取特定日期的日期数。所以我遍历数组,找到确切的日期,我需要知道同一天数组中有多少天。

4

2 回答 2

1

编辑

NSDate *sourceDate = theDateThatYouAreLookingFor;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];

NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:sourceDate];    

NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];

int count = 0;
//add check for existing dates on the same day
for(NSDate *checkDate in datesArray)
{
    dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:checkDate];    

    NSInteger checkDay = [dateComponents day];
    NSInteger checkMonth = [dateComponents month];
    NSInteger checkYear = [dateComponents year];

    if(checkDay == day && checkMonth == month && checkYear == year)
        count++;

}
//do something with count

结束编辑

最简单的方法是遍历数组并使用 NSDateFormatter 将每个日期的日期作为字符串进行比较。请记住,NSDateFormatters 很昂贵,因此您不想过于频繁地执行该过程。

你可以这样做:

NSMutableDictionary *dateInfo = [NSMutableDictionary dictionary];


NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = @"yyyyMMdd";

for(NSDate *date in datesArray)
{
    NSString *dateStr = [formatter stringFromDate:date];
    NSNumber *currentCount = [dateInfo objectForKey:dateStr];
    if(currentCount)
    {
        //bump value
        [dateInfo setObject:[NSNumber numberWithInt:currentCount.intValue + 1] forKey:dateStr];
    }
    else
    {
        [dateInfo setObject:[NSNumber numberWithInt:1] forKey:dateStr];
    }
}
于 2013-06-21T17:38:34.327 回答
1

出于性能原因,我将创建两个日期,“今天”(00:00:00)和“明天”(00:00:00)。您将这些日期用作比较的界限。如果日期等于或大于今天且小于明天,则为匹配。

由于 NSDate 比较基本上是双精度(原始类型)比较,因此该方法比比较 NSString 对象或 NSDateComponents 更快。创建 NSDateComponents 需要用到很多数学,而 NSDateFormatters 可能会使用 NSDateComponents 本身。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *todayComponents = [[NSDateComponents alloc] init];
todayComponents.year = 2013;
todayComponents.month = 6;
todayComponents.day = 21;

// today must be at the beginning of your search day. i.e. 00:00:00
NSDate *today = [calendar dateFromComponents:todayComponents]; 

NSDateComponents *oneDayOffset = [[NSDateComponents alloc] init];
oneDayOffset.day = 1;
NSDate *tomorrow = [calendar dateByAddingComponents:oneDayOffset toDate:today options:0];

NSIndexSet *todaysIndexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    if ([(NSDate *)obj compare:today] != NSOrderedAscending && [(NSDate *)obj compare:tomorrow] == NSOrderedAscending) {
//       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ obj is not earlier than "today" (= same time or later)
//                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ obj is earlier than "tomorrow"            
        return YES;
    }
    return NO;
}];

NSLog(@"%d", [todaysIndexes count]);

将今天替换为您的搜索日期(午夜),您就可以开始了。

于 2013-06-21T18:57:44.293 回答