开发者

Iterate over NSArray filled with dates and comparing to another NSArray

开发者 https://www.devze.com 2023-03-31 01:11 出处:网络
I have an NSArray filled with about 30 dates in the NSDate format, What I need to do is create another array from this, which contains a boolean for all of the dates from the first date to the last d

I have an NSArray filled with about 30 dates in the NSDate format,

What I need to do is create another array from this, which contains a boolean for all of the dates from the first date to the last date.

ex Array 1 1/1/11 3/1/11 5/1/11

Array 2 Yes No Yes No Yes

I need this for tapku library cale开发者_运维百科ndar

This is what I have so far but the i never changes

int i=0;
for (NSDate *date = [[startingDate copy] autorelease]; [date compare: endingDate] < 0;
     date = [date dateByAddingTimeInterval:24 * 60 * 60] ) {
    NSLog( @"%@ in [%@,%@]", date, startingDate, endingDate );

    int day1 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:[eventDates objectAtIndex:i]];
    int day2 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date];

    if(day1-day2==0) {
        NSLog(@"yeh");
        i=i+1;
        //add yes to array2

    } else {
        NSLog(@"nah");
        NSLog(@"%i",i);
        //add no to array 2
    }
}


I don't think I quite understand what you want to do, but I'll give it a try.

Suppose you have 2 NSDate's, call them date1 and date2, and you want to mark any entries in an array of NSDate's, call it arrayOfDates, that are between the 2 NSDate's, or not, and store the YES or NO values in another array, call it boolValuesOfDates. Then try this method:

- (void)compareDatesOfArrayFromDate:(NSDate *)date1 toDate:(NSDate *)date2; {

  NSDate * firstDate;
  NSDate * secondDate;

  if([date1 timeIntervalSinceDate:date2] >= 0){
    firstDate = date2;
    secondDate = date1;
  }
  else{
    firstDate = date1;
    secondDate = date2;
  }

  if(boolValuesOfDates != nil)
    [boolValuesOfDates release]; // Release the YES/NO array for the new values.

  boolValuesOfDates = [[NSMutableArray alloc] initWithCapacity:[arrayOfDates count]];

  BOOL isInbetweenDates;
  NSDate * curDate;

  for(int i = 0; i < [arrayOfDates count]; i++){
    curDate = [arrayOfDates objectAtIndex:i];
    isInbetweenDates = NO;
    if([curDate timeIntervalSinceDate:firstDate] >= 0){
      if([curDate timeIntervalSinceDate:secondDate] <= 0){
        isInbetweenDates = YES;
      }
    }
    [boolValuesOfDates addObject:[NSNumber numberWithBool:isInbetweenDates]];
  }
}

This method checks the order of the 2 entered NSDate's, and then compares the NSDate's in the array, and if they are in between, marks them with a YES, or NO otherwise. Hope that Helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消