在我的应用程序中,我正在做音频分析。每一秒,我都在调用一些方法,比如这个:
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
int countArray = [array count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [array valueForKeyPath:@"@avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:array];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
for (int j = 0; j < countArray / 3; j++)
{
[averageArray removeLastObject];
[averageArray removeObjectAtIndex:0];
}
NSNumber * average = [averageArray valueForKeyPath:@"@avg.doubleValue"];
return average;
}
}
问题是我有时会收到一个 NSRangeException 错误(在 1 分钟或几个小时后......这取决于......)说数组索引超出了界限。奇怪的是索引并没有越界......这些方法只在主线程上调用。在此先感谢您的帮助 !
编辑 1:在 Anim 和 Abhinav 的帮助下,我将代码更改如下。它已经工作了超过 2 小时 45 分钟(这是一个记录),然后因 EXC_BAD_ACCESS 代码 1 错误而崩溃......
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
NSArray *arrayCopy = [[NSArray alloc] initWithArray:array]; // CRASH with EXC_BAD_ACCESS error
int countArray = [arrayCopy count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [arrayCopy valueForKeyPath:@"@avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:arrayCopy];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
int startOfRange = countArray / 3;
int rangeLength = countArray - 2 * (countArray / 3);
NSArray* slicedArray = [averageArray subarrayWithRange:NSMakeRange(startOfRange, rangeLength)];
NSNumber * average = [slicedArray valueForKeyPath:@"@avg.doubleValue"];
return average;
}
}