looping through an NSMutableDictionary
Asked Answered
A

6

95

How do I loop through all objects in a NSMutableDictionary regardless of the keys?

Alible answered 12/10, 2010 at 11:55 Comment(0)
C
210

A standard way would look like this

for(id key in myDict) {
    id value = [myDict objectForKey:key];
    [value doStuff];
}
Cristiano answered 12/10, 2010 at 11:58 Comment(2)
If only there were a way of doing something like: for (id key, value in myDict) that would be perfect.Keshiakesia
Better to iterate through keys though (i.e. id key in myDict.allKeys), rather than the dictionary. This allows you to mutate the dictionary, which might be what you want to do.Hilar
A
30

you can use

[myDict enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
    // do something with key and obj
}];

if your target OS supports blocks.

Albina answered 2/8, 2012 at 8:51 Comment(0)
L
23

You can use [dict allValues] to get an NSArray of your values. Be aware that it doesn't guarantee any order between calls.

Legitimacy answered 12/10, 2010 at 12:3 Comment(5)
I would use this one over the (id key in dictionary), especially on a mutable dictionary, since the latter throws a nasty error if the dictionary is modified while being enumerated.Upbuild
"it doesn't guarantee" - this is frequently needed (most languages have a version of dictionary that DOES guarantee order) - does it in practice preserve order?Eliott
@Eliott On MacOS X, I had pretty mixed up order (not order of insertion, not alphabetic, nothing), but consistent between calls.Legitimacy
Why use a NSDictionary if the mappings don't matter?Hearken
@Hearken Usually it matters somewhere and doesn't elsewhere. You also get the unique key constraint that might be useful for some cases.Legitimacy
W
5
  1. For simple loop, fast enumeration is a bit faster than block-based loop
  2. It's easier to do concurrent or reverse enumeration with block-based enumeration than with fast enumeration When looping with NSDictionary you can get key and value in one hit with a block-based enumerator, whereas with fast enumeration you have to use the key to retrieve the value in a separate message send

in fast enumeration

for(id key in myDictionary) {
   id value = [myDictionary objectForKey:key];
  // do something with key and obj
}

in Blocks :

[myDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

   // do something with key and obj
  }];
Wongawonga answered 12/8, 2014 at 10:6 Comment(0)
A
3

You don't need to assign value to a variable. You can access it directly with myDict[key].

    for(id key in myDict) {
        NSLog(@"Key:%@ Value:%@", key, myDict[key]);
    }
Applicator answered 26/8, 2016 at 6:50 Comment(0)
A
2

Another way is to use the Dicts Enumerator. Here is some sample code from Apple:

NSEnumerator *enumerator = [myDictionary objectEnumerator];
id value;

while ((value = [enumerator nextObject])) {
    /* code that acts on the dictionary’s values */
}
Ardys answered 23/4, 2013 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.