【发布时间】:2011-09-06 17:36:30
【问题描述】:
我有一段代码在执行时给了我这个错误。而且我比较新,我似乎无法解决问题。
错误: 2011-09-06 12:31:06.094 ForceGauge[266:707] CoreAnimation:忽略异常:*-[NSMutableArray objectAtIndex:]:索引 1 超出范围 [0 .. 0]
-(void)peakCollector:(NSMutableArray *)testarray {
NSUInteger totalRows = [testarray count];
NSMutableArray *percentArray = [[NSMutableArray alloc]initWithObjects:0, nil];
if(forcecontroller.selectedSegmentIndex==0)
testarray = lbData;
else if(forcecontroller.selectedSegmentIndex==1)
testarray = kgData;
else if(forcecontroller.selectedSegmentIndex ==2)
testarray = ozData;
else if(forcecontroller.selectedSegmentIndex ==3)
testarray = newtonData;
for(int i = 0; i< totalRows-1; i++) {
if ([[testarray objectAtIndex:i+1] doubleValue] >= 1.2 * [[testarray objectAtIndex:i] doubleValue]) {
percentArray = [testarray objectAtIndex:i];
DatabaseTable *tableVC = [[DatabaseTable alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
if(forcecontroller.selectedSegmentIndex==0)
[tableVC copydatabase:percentArray];
else if(forcecontroller.selectedSegmentIndex==1)
[tableVC copydatabase:kgData];
else if(forcecontroller.selectedSegmentIndex==2)
[tableVC copydatabase:ozData];
else if(forcecontroller.selectedSegmentIndex==3)
[tableVC copydatabase:newtonData];
[tableVC release];
} else {
[analogData removeAllObjects];
}
}
}
【问题讨论】:
-
[[NSMutableArray alloc]initWithObjects:0, nil];。 NSArray 只能包含对象,不能包含原始类型,您需要将它们包装起来,例如[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0], nil]; -
@Bavarious 仍然给出同样的错误。
-
@ila 我觉得问题出在其他地方,这就是我删除答案的原因。不过,我写的(以及 albertamg 作为评论发布的)仍然有效。
-
即使 '0' 值确实必须封装在 NSNumber 中,异常的原因在其他地方,隐藏在一个棘手的打字问题中:请参阅下面的答案。
标签: iphone ios nsmutablearray