Am Mi,06.08.2008 um 16:48 schrieb Wayne Shao:



On Wed, Aug 6, 2008 at 7:41 AM, Negm-Awad Amin <negm- [EMAIL PROTECTED]> wrote:

Am Mi,06.08.2008 um 16:27 schrieb Wayne Shao:

Hi

Please see the question in the comments of the following code.

NSMutableDictionary* aDictionary = (initialized with some objects)

for (id theKey in aDictionary) {
 id anObject = [[aDictionary objectForKey:theKey] retain];

[aDictionary removeObjectForKey:theKey];
// Question: will this removal break or corrupt the loop of enumerating the
elements?

[anObject someMessage];
}

in java, Iterators allow the caller to remove elements from the underlying collection during the iteration. is there similar semantics in objective-c?
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_8_section_1.html#/ /apple_ref/doc/uid/TP30001163-CH18-SW1

Enumeration is "safe"—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised.

Oh. thanks for the info. Is there an alternative method to remove during a loop? (besides the option to remove elements outside of the loop).
Copy the collection in front of the loop and iterate through that copy. In your special case you can iterate through a keys-array:
NSArray* keys = [aDcitionary allKeys];
for( key in keys ) {
…

Amin






Amin

BTW: This looks like a memory leak …

fixed?

for (id theKey in aDictionary) {
 id anObject = [[aDictionary objectForKey:theKey] retain];

[aDictionary removeObjectForKey:theKey];
// Question: will this removal break or corrupt the loop of enumerating the
elements?

[anObject someMessage];
[anObject release];
}




--
Wayne Shao
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/negm-awad%40cocoading.de

This email sent to [EMAIL PROTECTED]

Amin Negm-Awad
[EMAIL PROTECTED]







--
Wayne Shao


Amin Negm-Awad
[EMAIL PROTECTED]




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to