Msencenb
Msencenb

Reputation: 5104

Enumerating over second level NSDictionaries

Let's say I have an NSDictionary with a nested NSDictionary like this (simplified, I have about 10 nested dictionaries):

key1={
  nesteddictionary={
    nestkey1 = "nested value";
    nestedkey2 = "another value";
  }
  nesteddictionary2={
    nestedkey3 = "want this too";
  }
}
key2="awesome"

calling [dictionary allKeys] only gives me 'key1' and 'key2'. Is there any easy way to loop over all keys inside of a nested dictionary like this? Even the nested ones?

Upvotes: 0

Views: 308

Answers (1)

adali
adali

Reputation: 5977

recursive function

- (void) findAllKey:(NSDictionary*)dic
{
    for ( NSString *key in [dic allKeys] )
    {
        NSLog(@"%@",key);

        if ( [[dic objectForKey:key] isKindOfClass:[NSDictionary class]] )
        {
            [self findAllKey:[dic objectForKey:key]];
        }
    }
}

it's Depth-First-Search

Upvotes: 1

Related Questions