直接上代碼吧
//1.字典遍歷NSDictionary *testDict = @{@"q":@"yy", @"g":@"gg"};//直接遍歷 [testDict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { }]; //帶有遍歷方案 [testDict enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { }]; /*備注:每次遍歷都會傳遞當前一組key,value值,BOOL * _Nonnull stop表示遍歷當前這組數據之后是否結束循環,當 *stop = YES 的時候,表示該遍歷循環終止。*///2.數組遍歷NSArray *testArray = @[@"a",@"b",@"c"];[testArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { }]; [testArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { }]; //備注:每次遍歷都會傳遞當前位數,和對應的元素 //3.NSSet 和NSMutableSet ,無序數組 NSMutableSet *testSet = [[NSMutableSet alloc]init]; [testSet enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { }]; [testSet enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { }]; //備注:每次遍歷都會傳遞當前對象新聞熱點
疑難解答