2010-11-17 91 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