简体   繁体   English

Xcode - 由于未捕获的异常'NSRangeException'而终止应用程序

[英]Xcode - Terminating app due to uncaught exception 'NSRangeException'

When i run the my app, in this code there's an error when i try to remove the element from array. 当我运行我的应用程序时,在此代码中,当我尝试从数组中删除元素时出现错误。 Why there's this error? 为什么会出现这个错误? And How do to resolve this problem? 以及如何解决这个问题?

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

} }

error message: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 3 beyond bounds [0 .. 1]' * First throw call stack: (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 错误消息: *由于未捕获的异常'NSRangeException'终止应用程序,原因:'* - [__ NSArrayM removeObjectAtIndex:]:索引3超出bounds [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:终止称为抛出异常

When i is equal to 3, you have already removed 3 items from your array (0, 1, 2). i等于3时,您已从阵列中删除了3个项目(0,1,2)。 Now there are two remaining at positions 0 and 1. If you try to remove one at position 3, you're outside the range. 现在还有两个位于0和1位置。如果你试图在位置3处移除一个,那么你就超出了范围。

Call removeAllObjects on the array instead of going through the loop. 调用数组上的removeAllObjects而不是遍历循环。

Lines of code like this are a bad idea: 像这样的代码行是一个坏主意:

for (int i =0; i< count; i ++) { 
        [self.weightHistory removeWeightAtIndex:i]; //error when i > 2
    }

What you are doing is modifying an array while you are enumerating it. 您正在做的是在枚举数组时修改数组。 Changing the array as you go through it is bound to cause problems. 在经历数组时更改数组肯定会导致问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于未捕获的异常&#39;NSRangeException&#39;xcode 5而终止了应用程序 - Terminating app due to uncaught exception 'NSRangeException' xcode 5 ***由于未捕获的异常&#39;NSRangeException&#39;而终止应用程序,原因:&#39;*** - *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** 帮助我...由于未捕获的异常&#39;NSRangeException&#39;终止应用, - Help me …Terminating app due to uncaught exception 'NSRangeException', Xcode-由于iPhone应用程序的未捕获异常而终止 - Xcode - Terminating due to uncaught exception for iphone app 由于未捕获的异常,Xcode终止应用程序 - xcode terminating app due to uncaught exception 由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[NSMutableArray objectAtIndex:]:索引26超出范围[0 .. 0]” - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 26 beyond bounds [0 .. 0]' 因未捕获的异常“NSRangeException”而终止应用程序会使Range或index超出范围 - Terminating app due to uncaught exception 'NSRangeException' gives Range or index out of bounds' 由于未捕获的异常&#39;NSRangeException&#39;终止应用程序,原因:&#39;*** - [NSMutableArray objectAtIndex:]:索引1超出边界[0 .. 0]&#39; - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]' iPhone-ReplaceScene错误:由于未捕获的异常&#39;NSRangeException&#39;,正在终止应用程序,原因:&#39;***-[NSMutableArray replaceObjectAtIndex:withObject:] - iphone - replaceScene error:Terminating app due to uncaught exception 'NSRangeException',reason:'***-[NSMutableArray replaceObjectAtIndex:withObject:] ***由于未捕获的异常&#39;NSRangeException&#39;而终止应用程序,原因:&#39;***-[NSMutableArray objectAtIndex:]:索引1超出范围[0 .. 0]&#39; - *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM