从 nsmutablearray 中删除两个对象

发布于 2025-01-03 05:48:47 字数 630 浏览 1 评论 0原文

我有一个包含五个对象的 NSMutableArray。我想在满足特定条件时删除两个对象。但它给了我一个错误-----* 由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[NSMutableArray objectAtIndex:]:索引3超出范围[0 .. 2 ]' 这是我的代码

  -(IBAction)buttonPressed1:(id)sender{
for (int i = 0; i < [objectArray1 count]; i++) {

    if ([[objectArray1 objectAtIndex:3] isEqualToString:@"xyz"]) 
    {

        NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:3];
        [indexes addIndex:4];
        [objectArray1 removeObjectsAtIndexes:indexes];
        NSLog(@"Hello %@",objectArray1 );
  }
}

,如果我删除 for{} 条件,它就可以正常工作。任何帮助将不胜感激。

I have a NSMutableArray with five objects. I want to remove two objects when a certain condition is fulfilled. But it is giving me an error-----* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
Here is my code

  -(IBAction)buttonPressed1:(id)sender{
for (int i = 0; i < [objectArray1 count]; i++) {

    if ([[objectArray1 objectAtIndex:3] isEqualToString:@"xyz"]) 
    {

        NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:3];
        [indexes addIndex:4];
        [objectArray1 removeObjectsAtIndexes:indexes];
        NSLog(@"Hello %@",objectArray1 );
  }
}

IF I remove for{} condition it is working fine. Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2025-01-10 05:48:47

如果您想删除索引 3 和 4 处的对象(就像您在这里所做的那样),那么不要在循环内执行此操作。您将获取包含 5 个对象的数组,并在第一次循环中删除其中的最后 2 个对象,从而在数组中留下 3 个对象。下次循环时,您将对数组索引 3 处的项目运行相同的检查,并且该数组不再具有该索引,因为您已将其删除。

If you want to remove the objects at indexes 3 and 4, as you seem to be doing here, then don't do it inside a loop. You are taking your array of 5 objects and removing the last 2 objects in it the first time through the loop, leaving you with 3 objects in your array. The next time through your loop you are running the same check on the item at array index 3, and the array no longer has that index because you've deleted it.

雨落星ぅ辰 2025-01-10 05:48:47

看来您要循环 4 次。
如果条件为真,则条件代码将执行 4 次。
您创建索引 4
你删除它。
第二次删除它时,就会发生崩溃。

如果我正确理解你想要做什么,这就是代码:

if ([[objectArray1 objectAtIndex:3] isEqualToString:@"xyz"] && 
   objectArray1.count == 5) {
   NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:3];
   [indexes addIndex:4];
   [objectArray1 removeObjectsAtIndexes:indexes];
   //less code:
   //[objectArray1 removeLastObject];
   //[objectArray1 removeLastObject];       
}

It seems you are going through the loop 4 times.
If the condition is true, the conditional code is going to be executed 4 times.
You create an index 4.
You remove it.
The second time you remove it, you get a crash.

If I understood correctly what you want to do, this is the code:

if ([[objectArray1 objectAtIndex:3] isEqualToString:@"xyz"] && 
   objectArray1.count == 5) {
   NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:3];
   [indexes addIndex:4];
   [objectArray1 removeObjectsAtIndexes:indexes];
   //less code:
   //[objectArray1 removeLastObject];
   //[objectArray1 removeLastObject];       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文