代码之家  ›  专栏  ›  技术社区  ›  Steven Fisher

在iPhone上在两个nsdates之间循环的最简单方法?

  •  5
  • Steven Fisher  · 技术社区  · 14 年前

    从一个日期循环到另一个日期最简单的方法是什么?

    我在概念上想要的是这样的东西:

    for (NSDate *date = [[startDate copy] autorelease]; [date compare: endDate] < 0;
         date = [date dateByAddingDays: 1]) {
        // do stuff here
    }
    

    当然,这行不通:没有 dateByAddingDays: . 即使是这样,也会留下大量的自动释放的对象等待它们的销毁。

    以下是我的想法:

    • 我不能只加一个 NSTimeInterval ,因为一天中的秒数可能有所不同。
    • 我可以把它分解成 NSDateComponents 再加上一天的组件,然后重新组装。但那是又长又难看的代码。

    所以我希望有人已经尝试了一些选择,并找到了一个好的。有什么想法吗?

    3 回复  |  直到 13 年前
        1
  •  7
  •   Steven Fisher    14 年前

    设置一天的日期组件常量并重复添加:

        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *oneDay = [[NSDateComponents alloc] init];
        [oneDay setDay: 1];
    
        for (id date = [[startDate copy] autorelease]; [date compare: endDate] <= 0;
            date = [calendar dateByAddingComponents: oneDay
                                             toDate: date
                                            options: 0] ) {
            NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
        }
    

    这仍然会留下自动释放对象的痕迹,但是 dateByAddingComponents:toDate:options: 负责。不确定能做些什么。

        2
  •  4
  •   william    13 年前

    如何使用 日期=[日期日期加上时间间隔:24*60*60] 相反?

    for (NSDate *date = [[startDate copy] autorelease]; [date compare: endDate] < 0;
     date = [date dateByAddingTimeInterval:24 * 60 * 60] ) {
        NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
    }
    
        3
  •  3
  •   Steven Fisher    14 年前

    将快速枚举添加到日期范围类:

    - (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state
                                      objects: (id *)stackbuf
                                        count: (NSUInteger)len;
    {
        NSInteger days = 0;
        id current = nil;
        id components = nil;
        if (state->state == 0)
        {
            current = [NSCalendar currentCalendar];
            state->mutationsPtr = &state->extra[0];
            components = [current components: NSDayCalendarUnit
                                    fromDate: startDate
                                      toDate: endDate
                                     options: 0];
            days = [components day];
            state->extra[0] = days;
            state->extra[1] = (uintptr_t)current;
            state->extra[2] = (uintptr_t)components;
        } else {
            days = state->extra[0];
            current = (NSCalendar *)(state->extra[1]);
            components = (NSDateComponents *)(state->extra[2]);
        }
        NSUInteger count = 0;
        if (state->state <= days) {
            state->itemsPtr = stackbuf;
            while ( (state->state <= days) && (count < len) ) {
                [components setDay: state->state];
                stackbuf[count] = [current dateByAddingComponents: components
                                                           toDate: startDate
                                                          options: 0];
                state->state++;
                count++;
            }
        }
        return count;
    }
    

    这很难看,但这种难看只限于我的约会课。我的客户代码只是:

    for (id date in dateRange) {
        NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
    }
    

    我认为这可能是一个足够好的理由来创建一个日期范围类,如果你还没有一个。