IOS for and Forin differences and considerations

Source: Internet
Author: User

One efficiency:

For VS for (...)

    1. For a wide range of applications such as Nsarray, Nsarray, and C-language arrays, and for (...) Limited to Nsarray, Nsarray, etc.
    2. For (...) more concise, more efficient

Test code:

10^7 Array, time unit seconds, precision milliseconds

  1. Nsmutablearray *test = [nsmutablearray array];
  2. For (int i= 0; i < 10000000; i++) {
  3. [Test addobject:@ (i)];
  4. }
  5. int sum = 0;
  6. Double date_s = cfabsolutetimegetcurrent ();
  7. For (int i = 0;i < test. Count; i++) {
  8. Sum + = 1;
  9. }
  10. Double date_e = cfabsolutetimegetcurrent ();
  11. NSLog (@ "Forloop time:%f", date_e-date_s);
  12. date_s = Cfabsolutetimegetcurrent ();
  13. For (ID obj in test) {
  14. Sum + = 1;
  15. }
  16. Date_e = Cfabsolutetimegetcurrent ();
  17. NSLog (@ "Enumeration time:%f", date_e-date_s);

Test results:

Taking into account the actual situation, forloop operation more.

Two The difference that is used For LoopIt is mainly used when you already know the number of cycles or the number of loops you can explicitly get, such as if you want to make 100 "Hello word!" on the screen or loop through the entire contents of an array, then you can use for (int i=0;i<100,i++) for (int i = 0; i < Array.count; i + +) Here you have made it very clear that the number of arrays that are 100 or get arrayFor in LoopIt is convenient to traverse an array that already knows the object type and does not care about the index, such as knowing that the array is UIButton so you can use for UIButton the *tmpbutton in Array so you can directly access each button in the array directly through the Tmpbutton the other array also has a block traversal method
1                 [array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {2                     //..... 3                 }];

One thing to note about this approach is that there may be problems with asynchronous execution. The last three kinds of traversal methods are for In is the highest efficiency--------------------------------------------------------------------------------------------for in Loop traversal is a bit more advanced than for a, and when you have an array of elements that are the same, you can iterate with for in, and accept the same type of object that is stored in the array.

three things to note

If the array is modified in the for-in loop, either increment, delete, or modify the position of the array element, it throws an exception, and the error is that the array being traversed has been destroyed (<__NSArrayM:0xa4fc000> was mutated While being enumerated. ), such as the following code:


Nsmutablearray* arr = [Nsmutablearray arraywithobjects:@ "1",@ "2",@ "3", Nil];

for (nsstring* str in arr) {

if ([str isequaltostring:@ "1"] | | [Str isequaltostring:@ "5"]) {

[Arr addobject:@ "4"];// [arr removeobject:@ "1"]; or [arr exchangeobjectatindex: 0 withobjectatindex:2];

continue;

}

}

Reason:

For in is actually a quick enumeration, which is different from the for loop.

One of the Nsarray enumeration operations needs to be noted: when enumerating through mutable arrays, you cannot alter the array container by adding or removing objects like this. If you do this, the enumerator will be confused and you will get undefined results.

There is a problem with the operation itself, the array container has changed, it may traverse to a location that is not assigned, and the For loop machine cannot detect it by itself, but the enumerator can detect it.

Append, this error (<__NSArrayM:0xa4fc000> was mutated while being enumerated. ) means that the array has mutated in the process of the enumeration

IOS for and Forin differences and considerations

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.