Simplest way to loop between two NSDates on iPhone?

后端 未结 3 884
长情又很酷
长情又很酷 2020-12-10 08:21

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] aut         


        
相关标签:
3条回答
  • 2020-12-10 08:49

    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.

    0 讨论(0)
  • 2020-12-10 09:00

    Set up a oneDay date component constant and repeatedly add it:

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

    This still leaves the trail of autoreleased objects, but dateByAddingComponents:toDate:options: is responsible. Not sure anything can be done about that.

    0 讨论(0)
  • 2020-12-10 09:03

    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 );
    }
    
    0 讨论(0)
提交回复
热议问题