数组反向遍历ios_iOS中数组遍历的方法及比较
數(shù)組遍歷是編碼中很常見(jiàn)的一種需求,我們來(lái)扒一拔iOS里面都有什么樣的方法來(lái)實(shí)現(xiàn),有什么特點(diǎn)。
因?yàn)閕os是兼容C語(yǔ)言的,所以c語(yǔ)言里面的最最常見(jiàn)的for循環(huán)遍歷是沒(méi)有問(wèn)題的。
本文中用的數(shù)組是獲取的系統(tǒng)的語(yǔ)言數(shù)組,大約有30多個(gè)數(shù)據(jù),雖然還不夠模擬大批量的數(shù)據(jù),但對(duì)于方法的驗(yàn)證是沒(méi)有問(wèn)題的了。
NSArray?*langArray?=?[[NSUserDefaultsstandardUserDefaults]arrayForKey:@"AppleLanguages"];
第一種方法是最最熟悉的C語(yǔ)言演化過(guò)來(lái)的:
for?(int?i?=?0;?i
NSLog(@"langArray[%d]=%@",?i,?langArray[i]);
}
這個(gè)方法最普通,效率也一般,但它也有好處,一是方便針對(duì)下標(biāo)的處理,就是說(shuō)如果我要處理i==10的情況是很簡(jiǎn)便的,另一個(gè)是可以比較方便的反向遍歷。
Objective-C?1.0里面的NSEnumerator也可以進(jìn)行遍歷,代碼如下:
NSEnumerator?*enumerator?=?[langArrayobjectEnumerator];
id?object;
while?((object?=?[enumeratornextObject])?!=nil)?{
NSLog(@"langArray=%@",?object);
}
這里我們可以看到?jīng)]有下標(biāo)了,通過(guò)nextObject的方法來(lái)遍歷。這個(gè)方法的好處是對(duì)于遍歷NSDictionary和NSSet代碼也比較類(lèi)似,不便的是對(duì)于下標(biāo)的處理會(huì)不方便,另外反向遍歷需要用reverseObjectEnumerator方法。
objective-c發(fā)展到2.0時(shí)又有了快速遍歷功能,代碼如下:
for?(id?object?in?langArray)?{
NSLog(@"langArray=%@",?object);
}
這里代碼簡(jiǎn)潔清晰,很長(zhǎng)時(shí)間是我寫(xiě)代碼的首選,號(hào)稱效率也最高,不過(guò)不便之處同樣明顯,如果算法要求知道數(shù)組的下標(biāo),這個(gè)方法就抓瞎了。另外,反向需要通過(guò)[langArray reverseObjectEnumerator]來(lái)實(shí)現(xiàn)。
等到block出來(lái)后,iOS里面新增加了enumerateObjectsUsingBlock:的方法,代碼如下:
[langArrayenumerateObjectsUsingBlock:^(id?obj,?NSUInteger?idx,?BOOLBOOL?*stop)?{
NSLog(@"idx=%d,?id=%@",?idx,?obj);
}];
這里我們看到block里面的參數(shù)包括object,下標(biāo)以及是否停止遍歷,應(yīng)該說(shuō),這個(gè)能滿足基本所有的遍歷需求了,有下標(biāo),有運(yùn)行的對(duì)象,還有是否繼續(xù)遍歷的標(biāo)志。不過(guò)反向遍歷呢?蘋(píng)果提供了另外一個(gè)方法:
[langArrayenumerateObjectsWithOptions:NSEnumerationReverseusingBlock:^(id?obj,?NSUInteger?idx,?BOOLBOOL?*stop)?{
NSLog(@"idx=%d,?id=%@",?idx,?obj);
}];
這個(gè)enumerateObjectsWithOptions:usingBlock:方法比enumerateObjectsUsingBlock:方法多傳了一個(gè)參數(shù),這個(gè)參數(shù)指定了遍歷的順序。
說(shuō)到這里,如果我們選擇正向遍歷,那么這兩種方法是一樣的么?答案也是否定的。在enumerateObjectsWithOptions:usingBlock:方法里面,如果指定了NSEnumerationConcurrent順序,那么底層通過(guò)GCD來(lái)處理并發(fā)執(zhí)行事宜,具體實(shí)現(xiàn)可能會(huì)用到dispatch group。也就是說(shuō),這個(gè)會(huì)用多線程來(lái)并發(fā)實(shí)現(xiàn),并不保證按照順序執(zhí)行,但效率肯定是杠杠的!
我們來(lái)看一下打印結(jié)果:
2014-06-1715:46:44.413?testStoryboard[2703:3503]?idx=32,?id=hu
2014-06-1715:46:44.413?testStoryboard[2703:1303]?idx=16,?id=ru
2014-06-1715:46:44.416?testStoryboard[2703:3503]?idx=33,?id=vi
2014-06-1715:46:44.412?testStoryboard[2703:60b]?idx=0,?id=zh-Hant
2014-06-1715:46:44.417?testStoryboard[2703:1303]?idx=17,?id=pl
2014-06-1715:46:44.417?testStoryboard[2703:60b]?idx=1,?id=zh-Hans
2014-06-1715:46:44.417?testStoryboard[2703:1303]?idx=18,?id=tr
2014-06-1715:46:44.419?testStoryboard[2703:60b]?idx=2,?id=en
2014-06-1715:46:44.419?testStoryboard[2703:1303]?idx=19,?id=uk
2014-06-1715:46:44.421?testStoryboard[2703:60b]?idx=3,?id=fr
2014-06-1715:46:44.421?testStoryboard[2703:1303]?idx=20,?id=ar
2014-06-1715:46:44.421?testStoryboard[2703:60b]?idx=4,?id=de
2014-06-1715:46:44.422?testStoryboard[2703:60b]?idx=5,?id=ja
2014-06-1715:46:44.422?testStoryboard[2703:60b]?idx=6,?id=nl
2014-06-1715:46:44.421?testStoryboard[2703:1303]?idx=21,?id=hr
2014-06-1715:46:44.423?testStoryboard[2703:60b]?idx=7,?id=it
2014-06-1715:46:44.423?testStoryboard[2703:1303]?idx=22,?id=cs
2014-06-1715:46:44.423?testStoryboard[2703:60b]?idx=8,?id=es
2014-06-1715:46:44.424?testStoryboard[2703:1303]?idx=23,?id=el
2014-06-1715:46:44.424?testStoryboard[2703:60b]?idx=9,?id=ko
2014-06-1715:46:44.424?testStoryboard[2703:1303]?idx=24,?id=he
2014-06-1715:46:44.425?testStoryboard[2703:60b]?idx=10,?id=pt
2014-06-1715:46:44.425?testStoryboard[2703:60b]?idx=11,?id=pt-PT
2014-06-1715:46:44.425?testStoryboard[2703:1303]?idx=25,?id=ro
2014-06-1715:46:44.426?testStoryboard[2703:60b]?idx=12,?id=da
2014-06-1715:46:44.426?testStoryboard[2703:1303]?idx=26,?id=sk
2014-06-1715:46:44.426?testStoryboard[2703:60b]?idx=13,?id=fi
2014-06-1715:46:44.426?testStoryboard[2703:1303]?idx=27,?id=th
2014-06-1715:46:44.427?testStoryboard[2703:60b]?idx=14,?id=nb
2014-06-1715:46:44.427?testStoryboard[2703:1303]?idx=28,?id=id
2014-06-1715:46:44.428?testStoryboard[2703:60b]?idx=15,?id=sv
2014-06-1715:46:44.428?testStoryboard[2703:1303]?idx=29,?id=ms
2014-06-1715:46:44.429?testStoryboard[2703:1303]?idx=30,?id=en-GB
2014-06-1715:46:44.429?testStoryboard[2703:1303]?idx=31,?id=ca
從這個(gè)結(jié)果我們可以看出,確實(shí)遍歷了整個(gè)數(shù)組,但并發(fā)按照順序從頭到尾——也就是說(shuō),用到了dispatch group。這在遍歷大數(shù)組而有相互獨(dú)立時(shí)對(duì)于效率的提高是相當(dāng)有利的,贊一個(gè)!
在iOS中,除數(shù)組外,還有NSDictionary和NSSet數(shù)據(jù)也是稱為collection數(shù)據(jù)的,遍歷有類(lèi)似的地方,不過(guò)遍歷沒(méi)有數(shù)組那么頻繁,方法上是差不多的。
總結(jié)
以上是生活随笔為你收集整理的数组反向遍历ios_iOS中数组遍历的方法及比较的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 10 i lt shell的if_she
- 下一篇: java编写自动化脚本生成apk_Uni