迭代时从NSMutableArray中删除的最佳方法是什么? - Best way to remove from NSMutableArray while iterating?

问题:

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? 在Cocoa中,如果我想循环遍历NSMutableArray并删除符合特定条件的多个对象,那么每次删除对象时如何在不重新启动循环的情况下执行此操作的最佳方法是什么?

Thanks, 谢谢,

Edit: Just to clarify - I was looking for the best way, eg something more elegant than manually updating the index I'm at. 编辑:只是为了澄清 - 我正在寻找最好的方式,例如比手动更新索引更优雅的东西。 For example in C++ I can do; 例如在C ++中,我可以做;

iterator it = someList.begin();

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

解决方案:

参考一: https://en.stackoom.com/question/T6I
参考二: https://stackoom.com/question/T6I
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章