代码之家  ›  专栏  ›  技术社区  ›  Mausimo

为什么在这个循环中要销毁NSMutableArray?

  •  0
  • Mausimo  · 技术社区  · 14 年前

    是阵列的性能被破坏了。

    这是数组的.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;
    }
    

    任何帮助都将不胜感激。我不明白为什么会这样?

    2 回复  |  直到 14 年前
        1
  •  5
  •   user467105user467105    14 年前

    这部分看起来不对:

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

    您允许+in it performanceItem,但随后将其设置为arrayofpperformances中的对象,然后释放它(当它指向arrayofpperformances中的对象时)。

    将该部分改为:

    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
    
        2
  •  0
  •   raidfive    14 年前

    我不认为被摧毁的表现。看起来你甚至没有在任何地方初始化它。