Array traversal is a common requirement for coding. We're going to take a pull on iOS. What are the ways to achieve this, and what are the characteristics of it?
Because iOS is compatible with C language. So the most common for loop traversal in C language is no problem.
The array used in this article is an array of the language of the obtained system, with approximately 30 or more data. Although it is not enough to simulate large quantities of data. However, there is no problem with validating the method.
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 advantages, one is convenient for the processing of subscript, that is to say that I have to deal with the situation is very simple i==10, there is a more convenient to reverse traversal.
OBJECTIVE-C 1.0 Inside the Nsenumerator is also able to traverse, code such as the following:
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 that it is also more similar to traversing nsdictionary and Nsset code, which is inconvenient for the processing of subscript. In addition, the Reverseobjectenumerator method is required for reverse traversal.
Objective-c developed to 2.0 with the high-speed traversal function, code such as the following:
For (ID object in Langarray) { NSLog (@ "langarray=%@", object); }
The code here is simple and clear, very long time is my first choice to write code. Claims to be the most efficient. Just the inconvenience is the same as obvious. Assuming that the algorithm requires knowing the subscript of the array, this method is flying blind. In addition, the reverse needs to be achieved by [Langarray Reverseobjectenumerator].
Wait until the block comes out. iOS adds a new Enumerateobjectsusingblock: method, code such as the following:
[Langarray enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { NSLog (@ "idx=%d, id=%@", idx, obj); }];
Here we see that the parameters in the block contain object. Subscript and whether to stop the traversal. It should be said that this can meet the basic all the traversal requirements, there is subscript. There is a flag that executes the object, and whether to continue the traversal. Just a 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 number of parameters, which specifies the order of traversal.
Here it is. Let's say we choose forward traversal. So are the two methods the same? The answer, too, is negative. In the Enumerateobjectswithoptions:usingblock: method, assume that the nsenumerationconcurrent order is specified. Then the bottom layer handles concurrency through GCD, and the detailed implementation may use the dispatch group.
In other words, this will be implemented concurrently with multithreading and is not guaranteed to run sequentially. But 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 follows the sequence from beginning to end-that is, the dispatch group is used. This is 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 also nsdictionary and nsset data, also known as collection data, that traverse a similar place, and simply traverse no arrays so frequently. method is almost the same.
The method and comparison of array traversal in iOS