Objective-C: `continue` in collection enumeration block? Objective-C: `continue` in collection enumeration block? ios ios

Objective-C: `continue` in collection enumeration block?


A block is a lot like an anonymous function. So you can use

return

to exit from the function with return type void.


Use "continue" when using Fast Enumeration to do this.

Sample code:

NSArray *myStuff = [[NSArray alloc]initWithObjects:@"A", @"B",@"Puppies", @"C", @"D", nil];for (NSString *myThing in myStuff) {    if ([myThing isEqualToString:@"Puppies"]) {        continue;    }    NSLog(@"%@",myThing);}

and output:

2015-05-14 12:19:10.422 test[6083:207] A2015-05-14 12:19:10.423 test[6083:207] B2015-05-14 12:19:10.423 test[6083:207] C2015-05-14 12:19:10.424 test[6083:207] D

Not a puppy in sight.


You can not use continue in block, you will get error: continue statement not within a loop. use return;

[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {        /* Do something with |obj|. */        if (idx==1) {            return;    }        NSLog(@"%@",obj);    }];