2010-11-17 38 views
0

這是arrayOfPerformances被打破。爲什麼NSMutableArray在這個循環中被破壞?

這裏是數組的.H:

NSMutableArray * arrayOfPerformances; 
} 
@property (nonatomic, retain) NSMutableArray * arrayOfPerformances; 

,並具有循環中的m:

[dataArray release]; 
[dataDictionary release]; 
dataArray = [[NSMutableArray alloc] init]; 
dataDictionary = [[NSMutableDictionary alloc] init]; 

NSDate * CurrentDate = start; 
int i = 0; 

NSMutableArray * arrayOfPerformancesForCurrentDate = [[NSMutableArray alloc] init]; 
while(YES) 
{ 
    i = 0; 
    if ([arrayOfPerformancesForCurrentDate count] > 0) 
    { 
     [arrayOfPerformancesForCurrentDate removeAllObjects]; 
    } 

    for (i; i < self.arrayOfPerformances.count; i++) 
    { 
     Performance * performanceItem = [[Performance alloc] init]; 
     performanceItem = [self.arrayOfPerformances objectAtIndex:i]; 

     NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10]; 
     NSString * sCurrentDate = [CurrentDate dateDescription]; 

     if([sPerformanceDate isEqualToString:sCurrentDate]) 
     { 
      [arrayOfPerformancesForCurrentDate addObject:performanceItem]; 
     } 

     [performanceItem release]; 
    } 

    if ([arrayOfPerformancesForCurrentDate count] >= 1) 
    { 
     [dataDictionary setObject:arrayOfPerformancesForCurrentDate forKey:CurrentDate]; 
     [dataArray addObject:[NSNumber numberWithBool:YES]]; 
    } 
    else 
    { 
     [dataArray addObject:[NSNumber numberWithBool:NO]]; 
    } 

    TKDateInformation info = [CurrentDate dateInformation]; 
    info.day++; 
    CurrentDate = [NSDate dateFromDateInformation:info]; 
    if([CurrentDate compare:end]==NSOrderedDescending) break; 
} 

任何幫助,將不勝感激。我不明白爲什麼會發生這種情況?

回答

5

這部分看起來不正確:

Performance * performanceItem = [[Performance alloc] init];  <-- 
performanceItem = [self.arrayOfPerformances objectAtIndex:i]; <-- 

NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10]; 
NSString * sCurrentDate = [CurrentDate dateDescription]; 

if([sPerformanceDate isEqualToString:sCurrentDate]) 
{ 
    [arrayOfPerformancesForCurrentDate addObject:performanceItem]; 
} 

[performanceItem release];          <-- 

你的alloc +初始化performanceItem但隨後將其設置爲arrayOfPerformances一個對象,然後將其釋放(而它的指向在arrayOfPerformances的對象)。

更改該節這樣的:

Performance *performanceItem = [self.arrayOfPerformances objectAtIndex:i]; 

NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10]; 
NSString * sCurrentDate = [CurrentDate dateDescription]; 

if([sPerformanceDate isEqualToString:sCurrentDate]) 
{ 
    [arrayOfPerformancesForCurrentDate addObject:performanceItem]; 
} 

//don't release performanceItem 
+0

乾杯,工作。我不知道爲什麼我分配和初始化Performance類,出於某種原因,我認爲我必須爲init分配一個空白的性能,以便爲它分配一個已經創建的對象。謝謝! – Mausimo 2010-11-17 06:23:59

0

我不認爲被破壞arrayOfPerformances。看起來你甚至沒有在任何地方初始化它。

+0

這不是完整的代碼,它在其他代碼中被初始化。通過這個部分,它有48個對象,並且當它穿過循環時,數組被破壞。 – Mausimo 2010-11-17 06:20:21

+0

對不起,我不知道丟失的代碼。 – raidfive 2010-11-17 06:45:09