Iterating through NSDictionary [#iosdev]

This was very helpful to me recently…

Iterating through NSDictionary

Iteration through NSDictionary could be achieved at least in two ways: using NSArray with [NSDictionary allKeys] or NSEnumerator.

Method 1:

NSArray *keyArray =  [bigUglyDictionary allKeys];
int count = [keyArray count];
for (int i=0; i < count; i++) {
  NSDictionary *tmp = [bigUglyDictionary objectForKey:[ keyArray objectAtIndex:i]];

Method 2:

NSEnumerator *enumerator = [bigUglyDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
  NSDictionary *tmp = [bigUglyDictionary objectForKey:key];

Second way is a little bit faster, so if you work with huge dictionaries and have no need of array with their keys – use it.


Hope it helps someone else too!