xcode 删除索引 NSMutableArray 处的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11457476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:52:18  来源:igfitidea点击:

Removing object at index NSMutableArray

objective-cxcode

提问by Alessandro

Possible Duplicate:
Objective-C NSMutableArray mutated while being enumerated?

可能的重复:
Objective-C NSMutableArray 在枚举时发生了变异?

I use this code to remove an object at index:

我使用此代码删除索引处的对象:

-(IBAction)deleteMessage:(id)sender{

UIButton *button = (UIButton*) sender;

for (UIImageView *imageView in imageArray) 
{

    if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == button.tag)
    {

        if (imageView.frame.size.height == 60) {
            x = 60;
        }

        if (imageView.frame.size.height == 200) {
            x = 200;
        }

        for (UITextView *text in messagetext) 
        {

            for (UITextView *name in messagename) 
            {

                if ([text isKindOfClass:[UITextView class]] && text.tag == button.tag && text.tag== name.tag)
                {

        [imageView removeFromSuperview];

                    [messagename removeObjectAtIndex:button.tag - 1];
                    [messagetext removeObjectAtIndex:button.tag - 1];

                 }
         }

 }

The error is:

错误是:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x704bdb0> was mutated while being enumerated.'

I noticed though that if I delete first the last object in the array, and go in order from last to firs, it works. But if I try removing an object at an index that is not the last, the app crashes and gives the error: (1,2,3,4..I delete object2... crash...if I delete object 4 no crash)

但我注意到,如果我首先删除数组中的最后一个对象,并从最后一个到第一个按顺序删除,它会起作用。但是,如果我尝试删除不是最后一个索引处的对象,应用程序会崩溃并给出错误:(1,2,3,4..I delete object2... crash...if I delete object 4 no碰撞)

回答by Alladinian

One way to do this is to make an array with the indexes you intend to remove, the you do your loop, add the indexes and remove the objects afterwards. Something like this:

一种方法是使用您打算删除的索引创建一个数组,然后执行循环,添加索引并随后删除对象。像这样的东西:

NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];

// Inside your loop
[indexes addIndex:(button.tag - 1)];

//..

// After your loop
[messagename removeObjectsAtIndexes:indexes];
[messagetext removeObjectsAtIndexes:indexes];

Now if you want different indexes for both arrays just make another NSMutableIndexSetand add the second set of indexes to it. Also don't forget to release indexesif you don't use ARC.

现在,如果您想要两个数组的不同索引,只需创建另一个NSMutableIndexSet并将第二组索引添加到其中。indexes如果您不使用 ARC,也不要忘记发布。

回答by jwj

You can't use "for each" style iteration on an array if you plan to mutate it (i.e., remove an element) because it messes with the iteration. If you really want to remove an element from the array as you iterate it, you need to use the "old style" iteration. Another Stack Overflow post heredoes a good job of showing how to use the old style that will allow you to mutate the array.

如果您计划对其进行变异(即删除一个元素),则不能在数组上使用“for each”样式迭代,因为它会干扰迭代。如果您真的想在迭代时从数组中删除一个元素,则需要使用“旧样式”迭代。此处的另一篇 Stack Overflow 帖子很好地展示了如何使用允许您更改数组的旧样式。

回答by EsbenB

You can't use "for x in y" iteration on an array if you insert or remove objects from it. Either you have to use a good ol' fashioned array or you could keep a reference to the object you want to remove and then remove it afterwards:

如果从数组中插入或删除对象,则不能在数组上使用“for x in y”迭代。要么你必须使用一个好的老式数组,要么你可以保留对要删除的对象的引用,然后再删除它:

NSObject *messageNameToRemove;
NSObject *messageTextToRemove; 
for (UITextView *text in messagetext) 
        {
        for (UITextView *name in messagename) 
        {

            if ([text isKindOfClass:[UITextView class]] && text.tag == button.tag && text.tag== name.tag)
            {

               [imageView removeFromSuperview];
               messageNameToRemove = [messagename objectAtIndex:button.tag -1];
               messageTextToRemove = [messagetext objectAtIndex:button.tag -1];
            }
         }

[messagename removeObject:messageNameToRemove];
[messagetext removeObject:messageTextToRemove];