iterating and removing objects from a collection
iterating and removing objects from a collection
- Subject: iterating and removing objects from a collection
- From: Dennis Munsie <email@hidden>
- Date: Mon, 30 Nov 2009 15:45:52 -0500
On a related, yet different note...
I run into this all the time where I need to iterate through an
NSMutableArray (or set, etc, etc) and remove some of the items. My normal
pattern has been this:
NSMutableSet *removeSet = [[NSMutableSet alloc] init];
for(NSObject *foo in myArray) {
if(needToRemoveFoo) {
[removeSet addObject:foo];
}
}
for(NSObject *foo in removeSet) {
[myArray removeObject:foo];
}
[removeSet release];
If I have to do multiple passes at the objects in myArray, I'll sometimes do
[removeSet removeAllObjects] instead of [removeSet release] and reuse the
NSMutableSet object as necessary.
Would it be better in general to make a copy of myArray in this case and
remove the objects directly from myArray as I iterate over the copy? Or are
the two methods generally equal in terms of performance and memory usage?
I am currently doing this on the iPhone, so I am concerned with my memory
and CPU usage more than I would be on the Mac. I also happen to be doing
this once every 30th or 60th of a second as part of my update loop in a
game.
Thanks!
On Mon, Nov 30, 2009 at 2:36 PM, Nick Zitzmann <email@hidden> wrote:
> What happens if you try enumerating on a copy of the sublayer array? That's
> what I usually do in this situation...
>
--
dennis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden