почему NSMutableArray уничтожается в этом цикле? - PullRequest
0 голосов
/ 17 ноября 2010

Разрушается массив arrayPofformances.

вот .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 ]

5 голосов
/ 17 ноября 2010

Эта часть не выглядит правильно:

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

Вы выделяете + init 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 голосов
/ 17 ноября 2010

Я не думаю, что arrayOfPerformances уничтожается. Похоже, вы даже нигде не инициализируете его.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...