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
Post a Comment