Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to loop between two NSDates on iPhone?

What's the simplest way to loop from one date to another?

What I want conceptually is something like this:

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

This doesn't work, of course: there's no dateByAddingDays:. And even if it did, it would leave a wide trail of autoreleased objects waiting for their destruction.

Here's what I've thought of:

  • I can't just add an NSTimeInterval, since the number of seconds in a day can vary.
  • I could break it down into NSDateComponents and add one day to the components, then reassemble it. But that's long and ugly code.

So I'm hoping someone has tried a few options for this, and found a good one. Any ideas?

like image 766
Steven Fisher Avatar asked Jul 28 '10 20:07

Steven Fisher


2 Answers

Add fast enumeration to a DateRange class:

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

This is ugly, but the ugliness is confined to my date range class. My client code is just:

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

I think this is probably a good enough reason to create a DateRange class if you don't have one already.

like image 120
Steven Fisher Avatar answered Jan 18 '23 22:01

Steven Fisher


How about using date = [date dateByAddingTimeInterval:24 * 60 * 60] instead?

for (NSDate *date = [[startDate copy] autorelease]; [date compare: endDate] < 0;
 date = [date dateByAddingTimeInterval:24 * 60 * 60] ) {
    NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
}
like image 24
william Avatar answered Jan 18 '23 23:01

william