当我运行我的应用程序时,在此代码中,当我尝试从数组中删除元素时出现错误。为什么会出现这个错误?以及如何解决这个问题?
if ([EnterWeightViewController withData]) {
Database *db = [[Database alloc] init];
[db openDB];
[db createTable];
NSArray *array = [db getAllPesi];
int count = [self.weightHistory countOfWeightHistory]; //count = 5
NSLog(@"Count of Array = %i", count);
//-- Init Array
for (int i =0; i< count; i ++) {
[self.weightHistory removeWeightAtIndex:i]; //error when i > 2
}
//-- Load Array
for (WeightEntry *entry in array) {
[self.weightHistory addWeight:entry];
}
//-- Reload Data
[self.tableView reloadData];
}
- (void)removeWeightAtIndex:(NSUInteger)weightIndex;{
// Manually send KVO messages.
[self willChange:NSKeyValueChangeRemoval
valuesAtIndexes:[NSIndexSet indexSetWithIndex:weightIndex]
forKey:KVOWeightChangeKey];
// Add to the front of the list.
[self.weightHistory removeObjectAtIndex:weightIndex];
// Manually send KVO messages.
[self didChange:NSKeyValueChangeRemoval
valuesAtIndexes:[NSIndexSet indexSetWithIndex:weightIndex]
forKey:KVOWeightChangeKey];
}
错误消息:* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM removeObjectAtIndex:]:索引 3 超出范围 [0 .. 1]” *第一次抛出调用堆栈:(0x19ba012 0x1397e7e 0x195c1c4 0x9618 0x5622 0x3c2753 0x3c2a7b 0x3d0590 0x3d85bd 0x3d8eab 0x3da3d6 0x3da675 0x96fd780 0x3d9625 0x3db728 0x35da0b 0x35d990 0x96d1f7a 0x360d1a 0x360f14 0x3610c9 0x31233f 0x312552 0x2f03aa 0x2e1cf8 0x26ecdf9 0x26ecad0 0x192fbf5 0x192f962 0x1960bb6 0x195ff44 0x195fe1b 0x26eb7e3 0x26eb668 0x2df65c 0x2b2d 0x2a55) libc++abi.dylib: terminate called throwing an exception