Fast Enumeration and remove elements
Fast Enumeration and remove elements
- Subject: Fast Enumeration and remove elements
- From: Trygve Inda <email@hidden>
- Date: Sun, 18 Jan 2015 12:45:25 -0800
- Thread-topic: Fast Enumeration and remove elements
Apple says:
> It is not safe to remove, replace, or add to a mutable collection’s elements
> while enumerating through it. If you need to modify a collection during
> enumeration, you can either make a copy of the collection and enumerate using
> the copy or collect the information you require during the enumeration and
> apply the changes afterwards. The second pattern is illustrated in Listing 4.
So is this safe...
NSMutableDictionary* collection; // keys are myID, values are MyObject
for (MyObject* object in [[self collection] allValues])
{
[collection removeObjectForKey:[object myID]];
}
It would seem that this is really enumberating the allValues array which
assuming it is only called once, represents the objects that exist at the
beginning of the for loop and removing them fro the collection dictionary is
safe.
Right?
Trygve
_______________________________________________
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