Practical tips for iOS development various traversal (iteration) methods in-objective-c
Description
1) This article briefly describes several common ways to traverse dictionaries, arrays, and collections in iOS development.
2) The corresponding code of this article can be obtained at the following address: Https://github.com/HanGangAndHanMeimei/Code
First, use for loop
To traverse a dictionary, an array, or a collection, the For loop is the simplest and most used method, as shown in the following example:
1//normal for Loop traversal 2-(void) Iteratorwithfor 3 {4//////////handling array//////////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//////////Processing Dictionary//////////13 nsdictionary *DICTM = @{@ "1": @ "one", @ "2": @ "", @ "3": @ "three"};14 Nsarray *DICTK Eysarray = [DICTM allkeys];15 for (int i = 0; i<dictkeysarray.count; i++) {NSString *key = Dictkeysarray [i];17 nsstring *obj = [DICTM objectforkey:key];18 NSLog (@ "%@:%@", key,obj); 19}20 21//////////Processing set Hopewell//////////22 Nsset * Setm = [[Nsset alloc] initwithobjects:@ "one" @ "one", @ "three", @ "four", nil];23 Nsarray *setob Jarray = [Setm allobjects];24 for (int i = 0; i<setobjarray.count; i++) {nsstring *obj = setobjarray[i]; NSLog (@ "%@", obj), 27}28 29//////////Reverse Traversal----descending----take an array as an example Nsarray *arrayM2 = @[@ "1", @ "2", @ "3", @ "4"];31 nsinteger arrayMCount2 = [arrayM2 count]-1;32 for (Nsinteger i = ArrayMCount2; i>0; i--) {NSString *obj = arraym2[i];35 NSLog (@ "%@", obj); 36}37}
Advantages: Simple
Disadvantage: Because the dictionary and the inside of the set are unordered, we need to use a new "array" as the intermediary to process the dictionary and the collection, which is a bit more overhead.
Second, using Nsenumerator traversal
The use of Nsenumerator is similar to the basic for loop, but the amount of code is much larger. Examples are as follows:
1//Using Nsenumerator traversal 2-(void) Iteratorwithenumerator 3 {4//////////processing array//////////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//////////processing Dictionary// Nsdictionary *DICTM = @{@ "1": @ "one", @ "2": @ "", @ "3": @ "three"};14 nsenumerator *dictenumerator = [DICTM Keyenumerator];15 NSString *key;16 while ((key = [Dictenumerator Nextobject])! = nil) {NSString *obj = Dictm[key];18 NSLog (@ "%@", obj); 19}20 21 22//////////Processing Set//////////23 nsset * Setm = [[Nsset alloc] I nitwithobjects:@ "One", @ "one", @ "three", @ "four", nil];24 nsenumerator *setenumerator = [Setm objectenumerator];25 NS String *setobj;26 while ((setobj = [Setenumerator nextobject])! = nil) {NSLog (@ "%@", setobj); 28}29 30 3 1//////////Reverse Traversal----descending----take an array as an exampleSarray *arraym2 = @[@ "1", @ "2", @ "3", @ "4"];33 nsenumerator *arrayenumerator2 = [ArrayM2 reverseobjectenumerator];34 NSString *obj2;35 while ((obj2 = [ArrayEnumerator2 nextobject])! = nil) {NSLog (@ "%@", obj2); 37}38 39}
Pros: For different data types, the traversal syntax is similar, and the internal can be easily reversed through the Reverseobjectenumerator setting.
Cons: The code size is slightly larger.
Third, use for ... In traversal
Added for in OBJECTIVE-C 2.0 ... In-form fast traversal. This kind of traversal way syntax concise, fast. Examples are as follows:
1//Use for ... In for fast traversal 2-(void) Iteratorwithforin 3 {4 //////////processing array//////////5 nsarray *arraym = @[@ "1", @ "2", @ "3", @ "4"]; 6 For (id obj in Arraym) {7 NSLog (@ "%@", obj), 8 } 9 //////////processing dictionary//////////11 nsdictionary * DICTM = @{@ "1": @ "one", @ "2": @ "", @ "3": @ "three"};12 for (id obj in DICTM) { NSLog (@ "%@", Dictm[obj]); }15 //////////processing set//////////17 nsset * Setm = [[Nsset alloc] initwithobjects:@ "one" @ "one", @ "three", @ " Four ", nil];18 for (id obj in Setm) { NSLog (@"%@ ", obj); }21 The inverse traversal----descending----The array as an example of nsarray *arraym2 = @[@ "1", @ "2", @ "3", @ "4"];24 for (id obj in [arrayM2 Reverseobjectenumerator]) { NSLog (@ "%@", obj); }27}
Advantages: 1) Concise grammar, 2) the most efficient;
Disadvantage: Unable to get subscript for the current traversal operation.
Four, block-based traversal mode
A block-based approach to traversal is the newest method introduced. It provides best practices for iterating through the array of data types such as dictionaries. Examples are as follows:
1//block-based Traversal mode 2-(void) Iteratorwithblock 3 {4//////////processing array//////////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//////////Processing dictionary//////////11 nsdictionary *DICTM = @{@ "1": @ "one", @ "2": @ "one", @ "3": @ "three"};12 [DICTM Enume ratekeysandobjectsusingblock:^ (ID _nonnull key, id _nonnull obj, BOOL * _nonnull stop) {NSLog (@ "%@:%@", Key,ob j); 14}];15 16//////////Processing Collection//////////17 Nsset * Setm = [[Nsset alloc] initwithobjects:@ "one" @ "one", @ "three" , @ "Four", nil];18 [Setm enumerateobjectsusingblock:^ (id _nonnull obj, BOOL * _nonnull stop) {NSLog (@ "%@", O BJ); 20}];21 22//////////Reverse Traversal----DESCENDING traversal----take an array as an example nsarray *arraym2 = @[@ "1", @ "2", @ "3", @ "4"];24 [arrayM2 en Umerateobjectswithoptions:nsenumerationreverse usingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _Nonnull sTop) {NSLog (@ "%zd--%@", idx,obj); 26}];27}
Pros: 1) You can get all the information you need directly from the block, including subscript, value, etc. In particular, there is no need to do extra coding to get the value of both key and value in relation to the dictionary.
2) The ability to directly modify the type of the key or obj in the block as the real type, can eliminate the work of type conversion.
3) The concurrency iteration function can be turned on by Nsenumerationconcurrent enumeration values.
Description: Block-based traversal is also very simple when implementing a reverse traversal, using the Enumerateobjectswithoptions method, passing nsenumerationreverse as a parameter, A block-based traversal is recommended when processing traversal operations.
V. To make the dispatch_apply function in GCD
The use of the Dispatch_apply function in GCD also enables the traversal of dictionaries, arrays, and so on, which is more suitable for dealing with situations that take a long time and have many iterations. Examples are as follows:
1//using dispatch_apply function in GCD 2-(void) Iteratorwithapply 3 {4 //////////processing array//////////5 nsarray *arraym = @[@ "1" , @ "2", @ "3", @ "4"]; 6 7 //Get global concurrent Queue 8 dispatch_queue_t queue = dispatch_get_global_queue (0, 0); 9 dispatch_apply ( Arraym.count, queue, ^ (size_t index) {One NSLog (@ "%@--%@", Arraym[index],[nsthread CurrentThread]); 13}
Advantage: Open multiple threads concurrently processing traverse task, the execution is high efficiency.
Cons: 1) The processing of dictionaries and collections requires an array; 2) cannot implement a reverse traversal.
Various traversal (iteration) modes in IOS-OBJECTIVE-C (reprint)