Re: Dictionary Enumeration
Re: Dictionary Enumeration
- Subject: Re: Dictionary Enumeration
- From: Ken Thomases <email@hidden>
- Date: Thu, 8 May 2008 18:18:04 -0500
On May 8, 2008, at 6:05 PM, Western Botanicals wrote:
I am getting the following error that seems to be happening because
I am enumerating through a dictionary while it is being edited. I
really need these things to happen simultaneously.
I doubt you do. If you think about it, the very idea of enumeration
breaks down if the dictionary is changing. Should newly added keys
appear in the enumeration?
What can I do?
That depends on what you're actually trying to accomplish. One
possible strategy is to use the -allKeys method to get a snapshot of
the keys in the dictionary prior to the operation. Enumerate over
that array and mutate the dictionary with what you find.
Heh. I wrote the above and then took a moment to look at the
documentation. In the description of the -keyEnumerator method, I
found this text which is so close to what I wrote that it's funny:
"If you use this method with instances of mutable subclasses of
NSDictionary, your code should not modify the entries during
enumeration. If you intend to modify the entries, use the allKeys
method to create a “snapshot” of the dictionary’s keys. Then use
this snapshot to traverse the entries, modifying them along the way."
Cheers,
Ken_______________________________________________
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