Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over an dictionary without knowing the keys, while getting key and object?

I have an NSDictionary and want to iterate over the objects. But at the same time, I need to know the key of the dictionary.

I remember there was a special, fancy form of fast enumeration, but have forgotten the exact syntax.

Anyone?

like image 849
dontWatchMyProfile Avatar asked Jun 17 '10 19:06

dontWatchMyProfile


2 Answers

I think what you want is something like this:

for (id key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

Taken from here.

like image 91
fbrereto Avatar answered Oct 28 '22 05:10

fbrereto


This is also a good choice if you like blocks.

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

}]
like image 42
zekel Avatar answered Oct 28 '22 04:10

zekel