Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSEnumerator with NSMutableDictionary?

Tags:

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

Thanks!

like image 740
Devoted Avatar asked Jun 30 '09 06:06

Devoted


1 Answers

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.

like image 103
Corey Floyd Avatar answered Sep 17 '22 12:09

Corey Floyd