說明:
1)該文簡短介紹在iOS開發中遍歷字典、數組和集合的幾種常見方式。
2)該文對應的代碼可以在下面的地址獲得:https://github.com/HanGangAndHanMeimei/Code
?
一、使用for循環
要遍歷字典、數組或者是集合,for循環是最簡單也用的比較多的方法,示例如下:
1 //普通的for循環遍歷 2 -(void)iteratorWithFor 3 { 4 //處理數組// 5 NSArray *arrayM = @[@"1",@"2",@"3",@"4"]; 6 NSInteger arrayMCount = [arrayM count]; 7 for (int i = 0; i<arrayMCount; i++) { 8 NSString *obj = arrayM[i]; 9 NSLog(@"%@",obj); 10 } 11 12 //處理字典// 13 NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"}; 14 NSArray *dictKeysArray = [dictM allKeys]; 15 for (int i = 0; i<dictKeysArray.count; i++) { 16 NSString *key = dictKeysArray[i]; 17 NSString *obj = [dictM objectForKey:key]; 18 NSLog(@"%@:%@",key,obj); 19 } 20 21 //處理集合// 22 NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil]; 23 NSArray *setObjArray = [setM allObjects]; 24 for (int i = 0; i<setObjArray.count; i++) { 25 NSString *obj = setObjArray[i]; 26 NSLog(@"%@",obj); 27 } 28 29 //反向遍歷----降序遍歷----以數組為例 30 NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"]; 31 NSInteger arrayMCount2 = [arrayM2 count] - 1; 32 33 for (NSInteger i = arrayMCount2; i>0; i--) { 34 NSString *obj = arrayM2[i]; 35 NSLog(@"%@",obj); 36 } 37 }
優點:簡單
缺點:由于字典和集合內部是無序的,導致我們在遍歷字典和集合的時候需要借助一個新的『數組』作為中介來處理,多出了一部分開銷。
?
二、使用NSEnumerator遍歷
NSEnumerator的使用和基本的for循環類似,不過代碼量要大一些。示例如下:
1 //使用NSEnumerator遍歷 2 -(void)iteratorWithEnumerator 3 { 4 //處理數組// 5 NSArray *arrayM = @[@"1",@"2",@"3",@"4"]; 6 NSEnumerator *arrayEnumerator = [arrayM objectEnumerator]; 7 NSString *obj; 8 while ((obj = [arrayEnumerator nextObject]) != nil) { 9 NSLog(@"%@",obj); 10 } 11 12 //處理字典// 13 NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"}; 14 NSEnumerator *dictEnumerator = [dictM keyEnumerator]; 15 NSString *key; 16 while ((key = [dictEnumerator nextObject]) != nil) { 17 NSString *obj = dictM[key]; 18 NSLog(@"%@",obj); 19 } 20 21 22 //處理集合// 23 NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil]; 24 NSEnumerator *setEnumerator = [setM objectEnumerator]; 25 NSString *setObj; 26 while ((setObj = [setEnumerator nextObject]) != nil) { 27 NSLog(@"%@",setObj); 28 } 29 30 31 //反向遍歷----降序遍歷----以數組為例 32 NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"]; 33 NSEnumerator *arrayEnumerator2 = [arrayM2 reverseObjectEnumerator]; 34 NSString *obj2; 35 while ((obj2 = [arrayEnumerator2 nextObject]) != nil) { 36 NSLog(@"%@",obj2); 37 } 38 39 }
優點:對于不同的數據類型,遍歷的語法相似;內部可以簡單的通過reverseObjectEnumerator設置進行反向遍歷。
缺點:代碼量稍大。
?
三、使用for...In遍歷
在Objective-C 2.0 中增加了for ...In 形式的快速遍歷。此種遍歷方式語法簡潔,速度飛快。示例如下:
1 //使用for...In進行快速遍歷 2 -(void)iteratorWithForIn 3 { 4 //處理數組// 5 NSArray *arrayM = @[@"1",@"2",@"3",@"4"]; 6 for (id obj in arrayM) { 7 NSLog(@"%@",obj); 8 } 9 10 //處理字典// 11 NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"}; 12 for (id obj in dictM) { 13 NSLog(@"%@",dictM[obj]); 14 } 15 16 //處理集合// 17 NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil]; 18 for (id obj in setM) { 19 NSLog(@"%@",obj); 20 } 21 22 //反向遍歷----降序遍歷----以數組為例 23 NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"]; 24 for (id obj in [arrayM2 reverseObjectEnumerator]) { 25 NSLog(@"%@",obj); 26 } 27 }
優點:1)語法簡潔;2)效率最高;
缺點:無法獲得當前遍歷操作所針對的下標。
?
四、基于Block的遍歷方式
基于Block的方式來進行遍歷是最新引入的方法。它提供了遍歷數組|字典等類型數據的最佳實踐。示例如下:
1 //基于塊(block)的遍歷方式 2 -(void)iteratorWithBlock 3 { 4 //處理數組// 5 NSArray *arrayM = @[@"1",@"2",@"3",@"4"]; 6 [arrayM enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 7 NSLog(@"%zd--%@",idx,obj); 8 }]; 9 10 //處理字典// 11 NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"}; 12 [dictM enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 13 NSLog(@"%@:%@",key,obj); 14 }]; 15 16 //處理集合// 17 NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil]; 18 [setM enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { 19 NSLog(@"%@",obj); 20 }]; 21 22 //反向遍歷----降序遍歷----以數組為例 23 NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"]; 24 [arrayM2 enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 25 NSLog(@"%zd--%@",idx,obj); 26 }]; 27 }
優點:1)遍歷時可以直接從block中獲得需要的所有信息,包括下標、值等。特別相對于字典而言,不需要做多余的編碼即可同時獲得key和value的值。
2)能夠直接修改block中key或者obj的類型為真實類型,可以省去類型轉換的工作。
3)可以通過NSEnumerationConcurrent枚舉值開啟并發迭代功能。
說明:基于Block的遍歷方式在實現反向遍歷的時候也非常簡單,使用enumerateObjectsWithOptions方法,傳遞NSEnumerationReverse作為參數即可,在處理遍歷操作的時候推薦基于Block的遍歷方式。
?
五、使GCD中的dispatch_apply函數
使用GCD中的dispatch_apply函數也能實現字典、數組等的遍歷,該函數比較適合處理耗時較長、迭代次數較多的情況。示例如下:
1 //使用GCD中的dispatch_apply函數 2 -(void)iteratorWithApply 3 { 4 //處理數組// 5 NSArray *arrayM = @[@"1",@"2",@"3",@"4"]; 6 7 //獲得全局并發隊列 8 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); 9 10 dispatch_apply(arrayM.count, queue, ^(size_t index) { 11 NSLog(@"%@--%@",arrayM[index],[NSThread currentThread]); 12 }); 13 }
優點:開啟多條線程并發處理遍歷任務,執行效率高。
缺點:1)對于字典和集合的處理需借助數組;2)無法實現反向遍歷。