5

ある日付から別の日付にループする最も簡単な方法は何ですか?

私が概念的に欲しいのは次のようなものです:

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

もちろん、これは機能しませんdateByAddingDays:。たとえそうなったとしても、自動解放されたオブジェクトが破壊されるのを待っている痕跡がたくさん残ります。

これが私が考えたものです:

  • NSTimeInterval1日の秒数はさまざまであるため、単に を追加することはできません。
  • 分解しNSDateComponentsてコンポーネントに 1 日追加してから、再組み立てすることができます。しかし、それは長くて醜いコードです。

だから私は誰かがこれのためにいくつかのオプションを試して、良いものを見つけたことを願っています. 何か案は?

4

3 に答える 3

7

oneDay 日付コンポーネント定数を設定し、繰り返し追加します。

    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:責任があります。それについて何かできるかどうかはわかりません。

于 2010-07-28T21:16:34.800 に答える
4

代わりにdate = [date dateByAddingTimeInterval:24 * 60 * 60]を使用するのはどうですか?

for (NSDate *date = [[startDate copy] autorelease]; [date compare: endDate] < 0;
 date = [date dateByAddingTimeInterval:24 * 60 * 60] ) {
    NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
}
于 2011-08-26T15:07:09.987 に答える
3

DateRange クラスに高速列挙を追加します。

- (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 );
}

これは、DateRange クラスをまだ作成していない場合に作成する十分な理由になると思います。

于 2010-07-28T22:51:10.357 に答える