objective c - Remove items in a for loop without side effects? -


can remove items looping through in objective-c for loop without side effects?

for example, ok?

for (id item in items) {    if ( [item customcheck] ) {       [items removeobject:item];   // ok here? } 

no, you'll error if mutate array while in fast enumeration loop. make copy of array, iterate on it, , remove original.

nsarray *itemscopy = [items copy];  (id item in itemscopy) {    if ( [item customcheck] )       [items removeobject:item];   // ok here }  [itemscopy release]; 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -