• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: remove elements during the iteration on NSMutableDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: remove elements during the iteration on NSMutableDictionary


  • Subject: Re: remove elements during the iteration on NSMutableDictionary
  • From: Negm-Awad Amin <email@hidden>
  • Date: Wed, 6 Aug 2008 17:04:21 +0200


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@hidden> 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).





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];
}

Oops, I diid not see that, sorry.

Yup, that works. I prefer:
[id anObject = [[aDictionary objectForKey:theKey] retain] autorelease];


That allows breaks inside the loop. (And you do not forget the - release, as you did.)

Amin






--
Wayne Shao
_______________________________________________

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

Amin Negm-Awad email@hidden







--
Wayne Shao


Amin Negm-Awad email@hidden




_______________________________________________

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


  • Follow-Ups:
    • Re: remove elements during the iteration on NSMutableDictionary
      • From: Roland King <email@hidden>
References: 
 >remove elements during the iteration on NSMutableDictionary (From: "Wayne Shao" <email@hidden>)
 >Re: remove elements during the iteration on NSMutableDictionary (From: Negm-Awad Amin <email@hidden>)
 >Re: remove elements during the iteration on NSMutableDictionary (From: "Wayne Shao" <email@hidden>)

  • Prev by Date: Re: remove elements during the iteration on NSMutableDictionary
  • Next by Date: Re: Protocol for Table View Trouble
  • Previous by thread: Re: remove elements during the iteration on NSMutableDictionary
  • Next by thread: Re: remove elements during the iteration on NSMutableDictionary
  • Index(es):
    • Date
    • Thread