***由于未捕获exception'NSRangeException',原因:'*** – :索引5超越界限'终止应用程序'

我得到*终止应用程序由于未捕获的exception'NSRangeException',原因:'*

我没有得到确切的问题,请帮助我

- (NSMutableArray*) arrayAtPointIndex:(NSInteger) index { NSMutableArray * array = [[NSMutableArray alloc] init]; if ( series ) { for (int plotIndex = 0; plotIndex <= [_points count]; plotIndex++) { SALChartCoordinate * coord1 = [_points objectAtIndex:plotIndex]; CGFloat differ = abs( [[coord1.series objectAtIndex:index] floatValue] ); [array addObject:[NSNumber numberWithFloat:differ]]; } } if ( [array count] > 0 ) return [array autorelease]; [array release]; return nil; } 

最可能的地方是: [coord1.series objectAtIndex:index]

因为你没有检查NSArray系列的大小和plotIndex。

把这样的条件

 if(coord1.series.count-1 <= index) { CGFloat differ = abs( [[coord1.series objectAtIndex:plotIndex] floatValue] ); [array addObject:[NSNumber numberWithFloat:differ]]; } 

注意 :您正在访问具有相同索引的数组和内部数组。 这不能肯定地说,但这是指示一些逻辑问题。 所以检查一下。

仔细检查..

 if ( series ) { for (int plotIndex = 1; plotIndex < [_points count]; plotIndex++) { SALChartCoordinate * coord1 = [_points objectAtIndex:plotIndex];//problem not here CGFloat differ = abs( [[coord1.series objectAtIndex:plotIndex] floatValue] );//problem is here check your array which contains only five objects but you are trying to get 6th object that is the reason. [array addObject:[NSNumber numberWithFloat:differ]]; } } 

Objective-C(以及大多数编程语言)中的数组索引从0开始。因此,5项目列表中的第一项是索引0,最后一项是索引4。

因此,for循环不应该从1开始,而是:

 for (int plotIndex = 0; plotIndex < [_points count]; plotIndex++) { ... 

但是,正如trojanfoe正确地提到的,这不是exception的原因。 您是否也可以打印出或检查以下值:

coord1.series

就像我想象的那样,这个系列中的物品数量与点数不一样,尽pipe你似乎依靠的是这样一个事实:

 CGFloat differ = abs( [[coord1.series objectAtIndex:plotIndex] floatValue] ); ^ coord1.series does not have an item at this index 

因此,我认为你的第一步是确定是否可以假设coord1.seriescoord1.series具有相同(或更多)的元素_points ,如果是的话,为什么它现在不会。

  - (NSMutableArray*) arrayAtPointIndex:(NSInteger) index { NSMutableArray * array = [[NSMutableArray alloc] init]; if ( series ) { for (int plotIndex = 0; plotIndex < [_points count]; plotIndex++) { SALChartCoordinate * coord1 = [_points objectAtIndex:plotIndex]; CGFloat differ = abs( [[coord1.series objectAtIndex:plotIndex] floatValue] ); [array addObject:[NSNumber numberWithFloat:differ]]; } } if ( [array count] > 0 ) return [array autorelease]; [array release]; return nil; 

}

试试这个代码…