View Full Version : NSDictionary Enumerations

North Bronson
Feb 22, 2011, 10:17 PM
I have an NSDictionary and I need to enumerate through the objects, but I don't need the keys. I see these two different ways:
[myDictionary enumerateKeysAndObjectsUsingBlock: ^(id key, id object, BOOL *stop) {

[object doSomething];


[[myDictionary allValues] enumerateObjectsUsingBlock: ^(id object, NSUInteger index, BOOL *stop) {

[object doSomething];

What's better here? Is it better to stick with the higher-level API that works directly on the dictionary, or tell the API that I'm going to treat the objects like an array?

Feb 22, 2011, 11:43 PM
If all you need are the objects, I'd probably just do:
for (id object in [myDictionary allValues])
That's even cleaner and easier to read (and 10.5 compatible ;)).