How iOS iterates through arrays

Source: Internet
Author: User

There are currently seven ways to know

First Type [arr enumerateobjectsusingblock: ^ (id obj, Nsuinteger idx, BOOL *stop) {NSLog (@ "%ld,%@", Idx,[arr Objectati    NDEX:IDX]);    }]; The second type of dispatch_apply ([arr Count], dispatch_get_global_queue (0, 0), ^ (size_t index) {//Parallel NSLog (@ "%ld,%@", Index,[a    RR Objectatindex:index]);    }); The third type of dispatch_apply ([arr Count], Dispatch_get_main_queue (), ^ (size_t index) {//serial, easy to cause the main thread to clog, can also open threads NSLog (@ "%ld    ,%@ ", Index,[arr Objectatindex:index]);    });    Fourth type for (Nsstring*str in arr) {NSLog (@ "%@", str);    }//fifth type, do-while int i = 0;        do {NSLog (@ "%@", [arr objectatindex:i]);    i++;        } while (I<[arr Count]);    The sixth type, while-do int j = 0;        while (J<[arr Count]) {NSLog (@ "%@", [arr objectatindex:j]);    j + +;    }//Seventh type, normal for loop for (int m = 0; M<[arr count]; m++) {NSLog (@ "%@", [arr objectatindex:m]); }

Personally prefer the first method

Attention:

① the second method is parallel, so the printed stuff is random, not printed in sequence.

② third is prone to clogging the main thread, so it's best to create a different thread yourself

③ I learned shallow, now know on these kinds of, welcome to add
③

How iOS iterates through arrays

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.