Objective-C Language Block Based Enumeration


Example

Enumerating dictionaries allows you to run a block of code on each dictionary key-value pair using the method enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block

Example:

NSDictionary stockSymbolsDictionary = @{
                                         @"AAPL": @"Apple",
                                         @"GOOGL": @"Alphabet",
                                         @"MSFT": @"Microsoft",
                                         @"AMZN": @"Amazon"
                                       };
NSLog(@"Printing contents of dictionary via enumeration");
[stockSymbolsDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Key: %@, Value: %@", key, obj);
}];