keyEnumerator
Returns an enumerator object that lets you access each key in the dictionary.
- (NSEnumerator *)keyEnumerator
Return Value
An enumerator object that lets you access each key in the dictionary.
Discussion of [NSDictionary keyEnumerator]
The following code fragment illustrates how you might use this method.
NSEnumerator *enumerator = [myDictionary keyEnumerator];
|
id key;
|
|
while ((key = [enumerator nextObject])) {
|
/* code that uses the returned key */
|
}
|
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 allKeys
method to create a “snapshot” of the dictionary’s keys. Then use this snapshot to traverse the entries, modifying them along the way.
Note that the
objectEnumerator
method provides a convenient way to access each value in the dictionary.Special Considerations
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 keyEnumerator]
NSEnumerator *enumerator = [myDictionary keyEnumerator];
for(NSString *aKey in enumerator) {
NSLog(@"%@", aKey);
NSLog(@"%@", [[myDictionary valueForKey:aKey] string]); //made up method
}
Example of [NSDictionary keyEnumerator]
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 keyEnumerator]
NSDictionary *myDict = ... some keys and values ...
NSEnumerator *keyEnum = [myDict keyEnumerator];
id key;
while ((key = [keyEnum nextObject]))
{
id value = [myDict objectForKey:key];
... do work with "value" ...
}