xcode 从 NSMutableArray 中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11442482/
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:51:49  来源:igfitidea点击:

remove object from NSMutableArray

objective-ciosxcodensmutablearray

提问by Ted

I have two elements:

我有两个要素:

NSMutableArray* mruItems;
NSArray* mruSearchItems;

I have a UITableViewthat holds the mruSearchItemsbasically, and once the user swipes and deletes a specific row, I need to find ALL matches of that string inside the mruItemsand remove them from there.

我有一个UITableView持有的mruSearchItems基本上,一旦用户扫描并删除特定行,我需要找到里面的那个字符串的所有比赛mruItems,并从那里删除它们。

I haven't used NSMutableArray enough and my code gives me errors for some reason:

我没有充分使用 NSMutableArray 并且我的代码由于某种原因给了我错误:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    //add code here for when you hit delete
    NSInteger i;
    i=0;
    for (id element in self.mruItems) {
        if ([(NSString *)element isEqualToString:[self.mruSearchItems objectAtIndex:indexPath.row]]) {

            [self.mruItems removeObjectAtIndex:i];
        }
        else
           {
            i++;
           }
    }
    [self.searchTableView reloadData];

}    

}

}

Error: I see now that some of the strings are not between quotation marks (the ones in UTF8 are though)

错误:我现在看到有些字符串不在引号之间(尽管 UTF8 中的字符串是)

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1a10e0> was mutated while being enumerated.(
    "\U05de\U05e7\U05dc\U05d3\U05ea",
    "\U05de\U05d7\U05e9\U05d1\U05d5\U05df",
    "\U05db\U05d5\U05e0\U05df",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    Hyman,
    Beans,
    Cigarettes
)'

回答by Nikolai Ruhe

You get an exception because you are mutating a container while iterating over its elements.

您会遇到异常,因为您在迭代容器元素时正在对其进行变异。

removeObject:does exactly what you're looking for: removing allobjects that are equal to the argument.

removeObject:正是您正在寻找的:删除所有与参数相等的对象。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle != UITableViewCellEditingStyleDelete)
        return;

    NSString *searchString = [self.mruSearchItems objectAtIndex:indexPath.row];
    [self.mruItems removeObject:searchString];
    [self.searchTableView reloadData];
}

回答by Stefan H

You cannot edit the collection while enumerating through it, instead, store the indexes away, and then remove them afterwards by looping through your array of indexes.

您不能在枚举时编辑集合,而是将索引存储起来,然后通过循环遍历索引数组来删除它们。