Thursday, May 9, 2013

NSDictionary objectEnumerator example ios


objectEnumerator

Returns an enumerator object that lets you access each value in the dictionary.
- (NSEnumerator *)objectEnumerator
Return Value
An enumerator object that lets you access each value in the dictionary.
Discussion
The following code fragment illustrates how you might use the method.
NSEnumerator *enumerator = [myDictionary objectEnumerator];
id value;
 
while ((value = [enumerator nextObject])) {
    /* code that acts on the dictionary’s values */
}
If you use this method with instances of mutable subclasses of NSDictionary, your code should not modify the entries during enumeration. If you intend to modify the entries, use the allValues method to create a “snapshot” of the dictionary’s values. Work from this snapshot to modify the values.
Special Considerations of [NSDictionary objectEnumerator]
It is more efficient to use the fast enumeration protocol (see NSFastEnumeration). Fast enumeration is available on OS X v10.5 and later and iOS 2.0 and later.
Example of [NSDictionary objectEnumerator]
id aKey = nil;
NSEnumerator *keyEnumerator = [paramaters keyEnumerator];
NSEnumerator *objectEnumerator = [paramaters objectEnumerator];
while ( (aKey = [keyEnumerator nextObject]) != nil) {
    id value = [objectEnumerator nextObject];
    NSLog(@"%@: %@", aKey, value);
}
Example of [NSDictionary objectEnumerator]
for (id d in [tmpDict objectEnumerator])
{
    // Objects in a JSON dictionary aren't necessarily dictionaries
    if([d isKindOfClass:[NSDictionary class]])
    {
         tmpID = [[d objectForKey:@"id"] integerValue];
    }
}