NSDictionary *myDictionary = ...;
for (id key in myDictionary) {
// do something with key here
id value = [myDictionary objectForKey:key];
// do something with value here
}
keyEnumerator
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
/* code that uses the returned key */
}
objectEnumerator
NSEnumerator *enumerator = [myDictionary objectEnumerator];
id value;
while ((value = [enumerator nextObject])) {
/* code that acts onthe dictionary’s values */
}
enumerateKeysAndObjectsUsingBlock:
[myDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop){
/* code that uses the returned key */
/* code that acts onthe dictionary’s values */
/* If the block sets *stop to YES, the enumeration stops. */
}]
enumerateKeysAndObjectsWithOptions:usingBlock:
block同上
options取值如下
enum {
NSEnumerationConcurrent = (1UL << 0),
NSEnumerationReverse = (1UL << 1),
};
typedef NSUInteger NSEnumerationOptions;
Constants
NSEnumerationConcurrent
Specifies thatthe Block enumeration should be concurrent.
The order of invocation is nondeterministic and undefined; this flag is a hint and may be
ignored bythe implementation under some circumstances; the code ofthe Block must be safe
against concurrent invocation.
Available in iOS 4.0and later.
NSEnumerationReverse
Specifies thatthe enumeration should be performed inreverse.
This option is available for NSArray and NSIndexSet classes; its behavior is undefined for
NSDictionary and NSSet classes, or when combined withthe NSEnumerationConcurrent flag.
Available in iOS 4.0and later.