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

How can I edit my VIM config so that Vim treats ".ejs" files the same as it currently treats my html files? -

Python __call__ special method practical example -