Array traversal is a very common requirement in coding, so we have to pick out what kind of method to implement and what the characteristics of iOS.
Because iOS is compatible with the C language, the most common for loop traversal in C is no problem.
The array used in this article is an array of system-acquired languages, about 30 or more data, although not enough to simulate a large amount of data, but the validation of the method is no problem.
Nsarray *langarray = [[Nsuserdefaults standarduserdefaults] arrayforkey:@ "Applelanguages"];
The first method is the most familiar C language evolved:
for (int i = 0; i<langarray.count; i++) = "" {NSlog (@ "langarray[%d]="%@ ", I,langarray[i]);}
This method is the most common, the efficiency is general, but it also has the advantage, one is convenient for the processing of subscript, that is, if I want to deal with the situation of i==10 is very simple, and the other is a more convenient reverse traversal.
OBJECTIVE-C 1.0 Inside the nsenumerator can also be traversed, the code is as follows:
Nsenumerator *enumerator = [Langarray objectenumerator]; ID object; while ((object = [Enumerator nextobject]) = nil) { NSLog (@ "langarray=%@", object); }
Here we can see there is no subscript, through the Nextobject method to traverse. The advantage of this method is to traverse the Nsdictionary and Nsset code is similar, inconvenient is the processing of the subscript will be inconvenient, in addition, the reverse traversal needs to use the Reverseobjectenumerator method.
Objective-c developed to 2.0 with the Quick Traverse function, the code is as follows:
For (ID object in Langarray) { NSLog (@ "langarray=%@", object); }
Here the code is concise and clear, long time is my first choice to write code, claiming that the efficiency is also the highest, but the inconvenience is equally obvious, if the algorithm requires knowledge of the array subscript, this method is flying blind. In addition, the reverse needs to be achieved by [Langarray Reverseobjectenumerator].
When the block comes out, iOS adds a new enumerateobjectsusingblock: The code is as follows:
[Langarray enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { NSLog (@ "idx=%d, id=%@", idx, obj); }];
Here we see the parameters inside the block including object, subscript and whether or not to stop the traversal, it should be said that this can meet the basic all the traversal requirements, there are subscript, there are running objects, and whether to continue to traverse the flag. But what about the reverse traversal? Apple offers another way:
[Langarray enumerateobjectswithoptions:nsenumerationreverse usingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { NSLog (@ "idx=%d, id=%@", idx, obj); }];
This enumerateobjectswithoptions:usingblock: method is more than Enumerateobjectsusingblock: The method passes a parameter, this parameter specifies the order of the traversal.
Speaking of which, if we choose forward traversal, are the two methods the same? The answer, too, is negative. In the Enumerateobjectswithoptions:usingblock: method, if the nsenumerationconcurrent order is specified, then the underlying is handled by GCD to handle concurrent execution. Specific implementations may use the dispatch group. In other words, this will be implemented concurrently with multithreading, not guaranteed in order to execute, but the efficiency is certainly the leverage!
Let's look at the print results:
2014-06-17 15:46:44.413 teststoryboard[2703:3503] idx=32, id=hu2014-06-17 15:46:44.413 testStoryboard[2703:1303] idx= ID=RU2014-06-17 15:46:44.416 teststoryboard[2703:3503] idx=33, ID=VI2014-06-17 15:46:44.412 testStoryboard[ 2703:60B] idx=0, id=zh-hant2014-06-17 15:46:44.417 teststoryboard[2703:1303] idx=17, ID=PL2014-06-17 15:46:44.417 TESTSTORYBOARD[2703:60B] idx=1, id=zh-hans2014-06-17 15:46:44.417 teststoryboard[2703:1303] idx=18, ID=TR2014-06-17 15:46:44.419 teststoryboard[2703:60b] idx=2, id=en2014-06-17 15:46:44.419 teststoryboard[2703:1303] idx=19, id= UK2014-06-17 15:46:44.421 teststoryboard[2703:60b] idx=3, id=fr2014-06-17 15:46:44.421 testStoryboard[2703:1303] idx= ID=AR2014-06-17 15:46:44.421 teststoryboard[2703:60b] idx=4, ID=DE2014-06-17 15:46:44.422 testStoryboard[2703:60b ] idx=5, ID=JA2014-06-17 15:46:44.422 teststoryboard[2703:60b] idx=6, ID=NL2014-06-17 15:46:44.421 testStoryboard[ 2703:1303] idx=21, id=hr2014-06-17 15:46:44.423 teststoryboard[2703:60b] idx=7, id=IT2014-06-17 15:46:44.423 teststoryboard[2703:1303] idx=22, ID=CS2014-06-17 15:46:44.423 testStoryboard[2703:60b] idx =8, ID=ES2014-06-17 15:46:44.424 teststoryboard[2703:1303] idx=23, ID=EL2014-06-17 15:46:44.424 testStoryboard[ 2703:60B] idx=9, id=ko2014-06-17 15:46:44.424 teststoryboard[2703:1303] idx=24, ID=HE2014-06-17 15:46:44.425 TESTSTORYBOARD[2703:60B] idx=10, id=pt2014-06-17 15:46:44.425 teststoryboard[2703:60b] idx=11, ID=PT-PT2014-06-17 15:46:44.425 teststoryboard[2703:1303] idx=25, id=ro2014-06-17 15:46:44.426 teststoryboard[2703:60b] idx=12, id= DA2014-06-17 15:46:44.426 teststoryboard[2703:1303] idx=26, ID=SK2014-06-17 15:46:44.426 testStoryboard[2703:60b] idx =13, ID=FI2014-06-17 15:46:44.426 teststoryboard[2703:1303] idx=27, ID=TH2014-06-17 15:46:44.427 testStoryboard[ 2703:60B] idx=14, id=nb2014-06-17 15:46:44.427 teststoryboard[2703:1303] idx=28, ID=ID2014-06-17 15:46:44.428 TESTSTORYBOARD[2703:60B] idx=15, id=sv2014-06-17 15:46:44.428 teststoryboard[2703:1303] idx=29, ID=MS2014-06-17 15:46:44.429 teststoryboard[2703:1303] idx=30, ID=EN-GB2014-06-17 15:46:44.429 testStoryboard[ 2703:1303] idx=31, ID=CA
From this result we can see that the entire array is actually traversed, but the concurrency is in order from beginning to end-that is, using the dispatch group. This is very advantageous for the improvement of efficiency when traversing large arrays and being independent of each other, praise one!
In iOS, in addition to the divisor group, there are nsdictionary and nsset data is also called collection data, traversal has a similar place, but the traversal is not as frequent as the array, the method is similar.
Methods and comparisons of array traversal in iOS