• 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: Searching dictionaries
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Searching dictionaries


  • Subject: Re: Searching dictionaries
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Sat, 11 Oct 2003 08:42:29 +0100

On Saturday, October 11, 2003, at 04:37 am, April Gendill wrote:

I have a data structure. The root is a dictionary which contains string items and other dictionaries (possibly nested). I tried enumerating the dictionary but I cannot remove an item or other wise use it because the enumerator only provides either a string or dictionary object.
I tried [theDict allKeysForObject:theObject];

Iterating through a dictionary generally looks something like the following:

NSEnumerator *keyEnum = [theDict keyEnumerator];
id key;

while ((key = [keyEnum nextObject])) {
id value = [theDict objectForKey:key];

/* Use key and value as desired */
}

Remember that you shouldn't rely on the order in which the keys are presented, as it may change depending on the implementation of the underlying dictionary; for example, if you need to examine the objects in key order, then you probably want to do something more like this:

NSArray *keys = [[theDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSEnumerator *keyEnum = [keys objectEnumerator];
id key;

while ((key = [keyEnum nextObject])) {
id value = [theDict objectForKey:key];

/* Use key and value as desired */
}

Note that for large dictionaries, that last example isn't very efficient (it copies all of the keys at least twice) and you might be better using an explicit tree implementation.

Kind regards,

Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Searching dictionaries
      • From: "Alastair J.Houghton" <email@hidden>
References: 
 >Searching dictionaries (From: April Gendill <email@hidden>)

  • Prev by Date: Re: Sending data to libxslt
  • Next by Date: Re: Searching dictionaries
  • Previous by thread: Re: Searching dictionaries
  • Next by thread: Re: Searching dictionaries
  • Index(es):
    • Date
    • Thread