2011-08-28 37 views
2

我有一個NSArray充滿在NSDate的格式約30日期,迭代的NSArray充滿日期,比較其他的NSArray

我需要做的就是創建一個從這個另一個數組,其中包含了一個布爾所有從第一次約會到最後約會的日期。

前陣1 1/1/11 11年3月1日 11年5月1日

陣列2 是 沒有 是 沒有 是

我需要這個tapku圖書館日曆

這是我到目前爲止,但我從來沒有變化

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

回答

1

我不認爲我很理解你想做什麼,但我會試一試。

假設你有2 NSDate的,叫他們date1date2,並要標記在NSDate陣列的任何條目的,把它arrayOfDates,這是2 NSDate的之間,或沒有,將YESNO值存儲在另一個陣列中,將其稱爲boolValuesOfDates。然後嘗試這個方法:

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

此方法檢查的2輸入NSDate訂單的,然後將NSDate比較的陣列中,並且如果它們之間,將它們標記與YES,或NO否則。希望有助於!

+0

這幾乎是我想要的所以感謝,但似乎給了我一個越界例外 – user913059

+0

如果你發佈的實際錯誤,它會更容易幫助你。此外,別忘了接受答案,以便其他人知道這是以後的幫助。 – msgambel

+0

我放棄了這種方法,決定嘗試一種不同的方法,但現在又出現了另一個問題! [鏈接](http://stackoverflow.com/questions/7222488/why-does-the-array-only-contain-the-final-mark) – user913059