陣列反向遍歷ios iOS中遍歷的方法總結

2021-10-13 00:27:12 字數 3945 閱讀 7880

在ios開發中,可以使用多種方法進行元素遍歷,具體有一下幾種:

經典for迴圈

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

for (int i = 0; i < iosarray.count; i++) {

//處理陣列中資料

nslog(@"%@", iosarray[i]);

nsenumerator遍歷

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

nsenumerator *enumerator = [iosarray objectenumerator];//正向遍歷

// nsenumerator *enumerator = [iosarray reverseobjectenumerator];//反向遍歷

id object;

while ((object = [enumerator nextobject]) != nil) {

//處理列舉器中的資料

nslog(@"%@", object);

for-in快速遍歷

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

for (nsstring *obj in iosarray) {

//處理陣列中的資料

nslog(@"%@", obj);

enumeratorblock遍歷

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

[iosarray enumerateobjectsusingblock:^(id _nonnull obj, nsuinteger idx, bool * _nonnull stop) {

nslog(@"%@", obj);

if ([obj isequaltostring:@"e"]) {

*stop = yes; // 跳出遍歷

另外,enumeratorblock還支援反向遍歷,併發遍歷,併發遍歷可以使用多核的優化,充分利用系統的資源。

反向遍歷

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

[iosarray enumerateobjectswithoptions:nsenumerationreverse usingblock:^(nsstring *obj, nsuinteger idx, bool * _nonnull stop) {

nslog(@"%@", obj);

if ([obj isequaltostring:@"e"]) {

*stop = yes;

併發遍歷

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

nsmutablearray *iosmutablearray = [nsmutablearray arraywitharray:iosarray];

[iosmutablearray enumerateobjectswithoptions:nsenumerationconcurrent usingblock:^(nsstring *obj, nsuinteger idx, bool * _nonnull stop) {

obj = [nsstring stringwithformat:@"_%@", obj];

[iosmutablearray replaceobjectatindex:idx withobject:obj];

nslog(@"%@", obj);

if ([obj isequaltostring:@"_i"]) {

*stop = yes;

dispatch_queue_t queue = dispatch_queue_create("queue", dispatch_queue_concurrent);

// dispatch_queue_t queue = dispatch_queue_create("queue", dispatch_queue_serial); // 序列佇列

enumerate *enumerate = [array objectatindex:i];

nslog(@"number: %ld", enumerate.number);

遍歷的注意事項

for迴圈中不要修改陣列

遍歷過程中是不能隨便刪除遍歷的元素的,如果需要刪除元素,可以先複製乙份出來,比如如下的**會有問題:

nsmutablearray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];

for (nsstring *obj in iosarray) {

//處理陣列中的資料

if([@"e" isequalto:obj]) {

[iosarray removeobject:obj];

但是使用enumerateblock可以在block內部做removeobject操作,原因應該是和block的特性有關, 在block中會儲存變數的值,而不會隨變數的值的改變而改變 。

遍歷的速率

當陣列容量很大的時候,如果只是進行陣列遍歷的話,使用for-in是最快速的,其次是併發遍歷,這個很多人都以為enumerateblock是最快的。

遍歷實踐tips

陣列分組

在開發中,有時需要對陣列進行某種情況的分組,比如,乙個擁有很多訊息模型的陣列,我們需要根據訊息的建立月份進行分組,那麼可以使用下面的方法實現:

nsmutableset *set=[nsmutableset set];

nsarray *array = @[message1, message2, message3, message4, message5, message6, message7];

__block nsarray *tempdataarray = [nsarray arraywitharray:array];

[tempdataarray enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) {

[set addobject:obj.month];//利用set不重複的特性,得到有多少組,根據陣列中訊息的月份屬性

[set enumerateobjectsusingblock:^(id obj, bool *stop) {//遍歷set陣列

nspredicate *predicate = [nspredicate predicatewithformat:@"self.month = %@", obj];//建立謂詞篩選器

nsarray *group = [tempdataarray filteredarrayusingpredicate:predicate];//用陣列的過濾方法得到新的陣列,在新增的最終的陣列

倒序遍歷

倒序遍歷也很常見,可以使用上面的反向遍歷來實現。

set排序

這個和emunerate其實沒有關係,但是也很實用,我們知道set是無序的,但是有時需要實現有順序的set,可以使用下面來實現:

//由於set無序,現將set轉換成nsarray

nsarray *sortdescriptor = @[[[nssortdescriptor alloc] initwithkey:@"self" ascending:no]];

nsarray *sortsetarray = [set sortedarrayusingdescriptors:sortdescriptor];

其實原理是將set轉化成array來實現的。

總結遍歷在我們日常開發中十分常見,根據應用場景,選擇合適的遍歷方法才是我們需要關係的。這篇文章總結了下遍歷的種類和注意事項,希望能幫助到有需要的同學。

陣列反向遍歷ios iOS開發 遍歷陣列

nsdictionary dictionary1 nsdictionary alloc initwithobjectsandkeys value0 key0 value1 key1 value2 key2 value3 key3 value4 key4 value4 key5 nil nslog d...

陣列中的遍歷

1 for of 與for in的區別 for of是es6中的,for in 是es3中的。for of 遍歷的是物件的鍵值,不會遍歷原型鏈,只會返回下標對應的屬性。for in遍歷的是物件的鍵名,會遍歷原型鏈 效能很差 會返回所有可列舉的屬性。注 in 大部分用於遍歷物件,of不能遍歷物件 le...

php遍歷mysql陣列中 遞迴遍歷PHP多維陣列

陣列的遍歷是php乙個常見的程式設計任務,而陣列又分為一維陣列 二維陣列和多維陣列。一維陣列的遍歷很簡單,直接乙個for迴圈就可以完成。那麼二維陣列和多維陣列的遍歷又應該如何實現呢?請看以下程式 author nowamagic url www.nowamagic.net date 2011 03 ...