Iteration through NSDictionary could be achieved at least in two ways: using NSArray with [NSDictionary allKeys] or NSEnumerator.
Snippets:
NSArray *keyArray = [bigUglyDictionary allKeys];
int count = [keyArray count];
for (int i=0; i < count; i++) {
NSDictionary *tmp = [bigUglyDictionary objectForKey:[ keyArray objectAtIndex:i]];
}
NSEnumerator *enumerator = [bigUglyDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
NSDictionary *tmp = [bigUglyDictionary objectForKey:key];
}
Second way is a little bit faster, so if you work with huge dictionaries and have no need of array with their keys – use it.
Tags: iphone-dev, NSDictionary, NSEnumerator
thanks, very helpful
Or:
for( NSString *aKey in [bigUglyDictionary allKeys] )
{
// do something
}
Or somewhat faster:
for( NSString *aKey in bigUglyDictionary )
{
// do something
}
Or better yet, if your target OS supports blocks:
[bigUglyDictionary enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
// do something with key and obj
}];
you have got an ideal blog here! would you wish to make some invite posts on my weblog?
Thanks (also Alan), that piece of code was very helpful.
Sir I’m using NSString* name = (NSString *)ABRecordCopyCompositeName(person);
to store contacts name from iphone contacts and I want that in an array so that it stores in array[0],array[1] and soon so that I can match it with some other array. Can u plz explain it to me.