How to stop a quick listing?

How would you end a quick census after you got what you were looking for.

In the for loop, I know that you just set the counter number to a thousand or something like that. Example:

for (int i=0;i<10;i++){ if (random requirement){ random code i=1000; } } 

therefore, without converting a fast enumeration to an object such as a direct loop (by comparing with [array count] , how can you stop a fast enumeration in the process?

+6
source share
4 answers

from documents

 for (NSString *element in array) { if ([element isEqualToString:@"three"]) { break; } } 

if you want to complete the enumeration when reaching a specific index, a block enumeration might be better, since it gives you an index by enumerating:

 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { //… if(idx == 1000) *stop = YES; }]; 
+10
source
 for (id object in collection) { if (condition_met) { break; } } 
+2
source

Could you just use the break statement?

 for (int x in /*your array*/){ if (random requirement){ random code break; } } 
+1
source

Just adding that for nested loops, a break in the inner loop only interrupts this loop. External cycles will continue. If you want to break free, you can do it like this:

 BOOL flag = NO; for (NSArray *array in arrayOfArrays) { for (Thing *thing in array) { if (someCondition) { // maybe do something here flag = YES; break; } } if (flag) { break; } } 
+1
source

Source: https://habr.com/ru/post/922753/


All Articles