在迭代时从NSMutableArray中删除最好的方法? [英] Best way to remove from NSMutableArray while iterating?

查看:90
本文介绍了在迭代时从NSMutableArray中删除最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cocoa中,如果我想循环访问一个NSMutableArray并删除符合一定条件的多个对象,最好的方法是在每次删除对象时不重新启动循环?



感谢,



编辑:只是为了澄清 - 我在寻找最好的方式,例如比我手动更新索引更优雅。例如在C ++中我可以做;

  iterator it = someList.begin 

while(it!= someList.end())
{
if(shouldRemove(it))
it = someList.erase
}


解决方案

一个初始循环,其中我收集要删除的项目。然后我删除它们。下面是一个使用Objective-C 2.0语法的例子:

  NSMutableArray * emitItems = [NSMutableArray array]; 
SomeObjectClass * item;

for(item in originalArrayOfItems){
if([item shouldBeDiscarded])
[ignoredItems addObject:item];
}

[originalArrayOfItems removeObjectsInArray:abandItems];

然后,没有问题是否正确更新索引或其他小的簿记详细信息。 p>

编辑以添加:



在其他答案中已经注意到,逆公式应该更快。即如果你遍历数组并组成一个新的要保留的对象数组,而不是丢弃对象。这可能是真的(虽然分配一个新的数组的内存和处理成本,并放弃旧的数组),但即使它更快,它可能不会像一个朴素的实现一样大,因为NSArrays不像正常数组那样表现。他们谈论谈话,但他们走不同的步行。请在这里查看好的分析:



http://ridiculousfish.com/blog/archives/2005/12/23/array/



逆公式可能更快,但我从来不需要关心是否,因为上面的表述总是足够快,我的需要。



对我来说,收起信息是使用无论什么配方是最清楚的。仅在必要时进行优化。我个人发现上面的配方最清晰,这就是为什么我用它。但是如果反演公式对你更清楚,那就去吧。


In Cocoa, if I want to loop through an NSMutableArray and remove multiple objects that fit a certain criteria, what's the best way to do this without restarting the loop each time I remove an object?

Thanks,

Edit: Just to clarify - I was looking for the best way, e.g. something more elegant than manually updating the index I'm at. For example in C++ I can do;

iterator it = someList.begin();

while (it != someList.end())
{
    if (shouldRemove(it))   
        it = someList.erase(it);
}

解决方案

For clarity I like to make an initial loop where I collect the items to delete. Then I delete them. Here's a sample using Objective-C 2.0 syntax:

NSMutableArray *discardedItems = [NSMutableArray array];
SomeObjectClass *item;

for (item in originalArrayOfItems) {
    if ([item shouldBeDiscarded])
        [discardedItems addObject:item];
}

[originalArrayOfItems removeObjectsInArray:discardedItems];

Then there is no question about whether indices are being updated correctly, or other little bookkeeping details.

Edited to add:

It's been noted in other answers that the inverse formulation should be faster. i.e. If you iterate through the array and compose a new array of objects to keep, instead of objects to discard. That may be true (although what about the memory and processing cost of allocating a new array, and discarding the old one?) but even if it's faster it may not be as big a deal as it would be for a naive implementation, because NSArrays do not behave like "normal" arrays. They talk the talk but they walk a different walk. See a good analysis here:

http://ridiculousfish.com/blog/archives/2005/12/23/array/

The inverse formulation may be faster, but I've never needed to care whether it is, because the above formulation has always been fast enough for my needs.

For me the take-home message is to use whatever formulation is clearest to you. Optimize only if necessary. I personally find the above formulation clearest, which is why I use it. But if the inverse formulation is clearer to you, go for it.

这篇关于在迭代时从NSMutableArray中删除最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆