Methods and comparisons of traversing arrays in IOS development

Source: Internet
Author: User

Array, as a kind of commonly used data type, frequently appear in the code, which certainly does not have an array of traversal, this blog log group traversal, do their own induction, if it is Daniel, a smile and pass on the good, learn from each other, welcome correction.

Words don't speak more directly into the subject

First create an array

/** gets the language array of the system */nsarray *languagearray = [[Nsuserdefaults standarduserdefaults] arrayforkey:@ "Applelanguages"];
    1. For loop-C language

Because the C language is supported in Xcode compilation, the first most common traversal method is the for loop and the most commonly used array traversal method.

 for (int0; i < languagearray.count; i++) {        NSLog (@ "languagearray[%d] =%@  ",  I,  languagearray[i]);    }

The resulting print results are languagearray[corresponding to the following table] = values in the array

This method is simple, the efficiency is general, the merit has three kinds,

First, to facilitate the processing of the subscript;

Second, can be convenient to carry out the reverse traversal;

Third, two for loop nesting, you can do bubble sort, bubble sort code, no suffix.

2.for...in Loop, also known as Fast for loop (objective-c-2.0 Time Out method)

 for (IDobject in languagearray) {        NSLog (@ "languagearray=%@ " Object );    }

This traversal method, known as efficiency first, is also my preferred encoding, set hired for loop, more concise, but need an external variable to achieve, inconvenience, temporarily met two kinds:

First, if the code needs to know the subscript, this method will be significantly less useful.

Second, the reverse traversal needs to be achieved by [Languagearray Reverseobjectenumerator], the need for the moment, this method I just know, and have not used.

3, IOS should be at the 2011 Developer Conference introduced block block usage, so far, for many years, the industry's evaluation of block blocks is mixed, a foreign programmer also dedicated to create a website called Fuckblock, to attack the IOS launched block , however, block is really good at optimizing the code to improve compilation efficiency, with two blocks in the array traversal: "Enumerateobjectsusingblock" and "Enumerateobjectswithoptions: Usingblock ", the former is generally used for positive sequence traversal, the latter for reverse traversal.

First of all, "Enumerateobjectsusingblock."

[Languagearray enumerateobjectsusingblock:^ (ID obj, nsuinteger idx, BOOL *stop) {        NSLog (@ "  idx=%d, id=%@", idx, obj);    }];

The parameters inside the "Enumerateobjectsusingblock" block include object, subscript, and whether to stop the traversal and "Enumerateobjectswithoptions:usingblock" Passed a parameter that specifies the traversal order.

[Languagearray enumerateobjectswithoptions:nsenumerationreverse usingblock:^ (ID obj, nsuinteger idx, BOOL * stop) {        NSLog (@ "idx=%d, id=%@", idx, obj);    }];

"Enumerateobjectswithoptions:usingblock" is not recommended when traversing a decimal group, the efficiency gap is not obvious, but it is recommended to use the block block when traversing a large array, using GCD to handle concurrent execution at the block bottom , so it should be useful to dispatch group to implement, which has a strong efficiency advantage when traversing large arrays.

Please correct me and learn together.

Methods and comparisons of traversing arrays in IOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.