基於塊的列舉
列舉詞典允許你使用方法 enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
在每個字典鍵值對上執行程式碼塊
例:
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);
}];