迭代時從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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章