20

How do I use NSEnumerator with NSMutableDictionary, to print out all the keys and values?

Thanks!

Devoted
  • 177,705
  • 43
  • 90
  • 110

3 Answers3

94

Unless you need to use NSEnumerator, you can use fast enumeration (which is faster) and concise.

for(NSString *aKey in myDictionary) {
    NSLog(@"%@", aKey);
    NSLog(@"%@", [[myDictionary valueForKey:aKey] string]); //made up method
}

Also you can use an Enumerator with fast enumeration:

NSEnumerator *enumerator = [myDictionary keyEnumerator];

for(NSString *aKey in enumerator) {
    NSLog(@"%@", aKey);
    NSLog(@"%@", [[myDictionary valueForKey:aKey] string]); //made up method
}

This is useful for things like doing the reverse enumerator in an array.

Patrick Pijnappel
  • 7,317
  • 3
  • 39
  • 39
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
  • 3
    Don't forget you can also enumerate values only from a dictionary if you want: for (NSString *value in [myDictionary allValues]). – Kendall Helmstetter Gelner Jun 30 '09 at 15:35
  • True, but -allValues does create an intermediate (autoreleased) array, so be aware of the extra storage required. – Quinn Taylor Jul 06 '09 at 19:31
  • 1
    `NSLog(@"%@)` instead of `NSLog("%@)`. – ohho Feb 08 '12 at 13:25
  • 1
    @KendallHelmstetterGelner You also can use [myDictionary objectEnumerator] to enumerate over all objects, which doesn't create an intermediate array. – Patrick Pijnappel Apr 26 '12 at 06:48
  • 1
    Also, it is perfectly fine to put an enumerator accessor directly inside the for statement,e.g. `for(NSString *aKey in [myDictionary keyEnumerator])`, since the expression after `in` is only evaluated once. – Patrick Pijnappel Apr 26 '12 at 06:53
  • Thanks for the note about objectEnumerator. The NSEnumerator I left separate so people would know the type of the thing being returned by that call and could look up docs, in real use I probably would combine that into the for statement as you suggest. – Kendall Helmstetter Gelner Apr 26 '12 at 23:30
8

From the NSDictionary class reference:

You can enumerate the contents of a dictionary by key or by value using the NSEnumerator object returned by keyEnumerator and objectEnumerator respectively.

In other words:

NSEnumerator *enumerator = [myMutableDict keyEnumerator];
id aKey = nil;
while ( (aKey = [enumerator nextObject]) != nil) {
    id value = [myMutableDict objectForKey:anObject];
    NSLog(@"%@: %@", aKey, value);
}
BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • You can also enumerate the keys in the same order with fast enumeration in Objective-C 2.0 — just use for (NSString *key in myMutableDict) { ... } instead. – Quinn Taylor Jul 06 '09 at 19:33
3

Here is the version without object search. Notice that objectForKey calling not exist. They use keyEnumerator and objectEnumerator both.

id aKey = nil;
NSEnumerator *keyEnumerator = [paramaters keyEnumerator];
NSEnumerator *objectEnumerator = [paramaters objectEnumerator];
while ( (aKey = [keyEnumerator nextObject]) != nil) {
    id value = [objectEnumerator nextObject];
    NSLog(@"%@: %@", aKey, value);
}
İzzet Okbay
  • 61
  • 1
  • 3
  • 1
    Is the order guaranteed? I.e., are you sure the value returned by the objectEnumerator is linked to the value returned by the keyEnumerator? – Steven Kramer Mar 21 '14 at 08:25