Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray enumerateObjectsUsingBlock is not synchronous as Apple says

Is this a bug?

I have this lines:

[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

     NSLog(@"%d", idx);
}];

NSLog(@"end");

This should print like this

"0"
"1"
"2"
...
"end"

but it is printing like

"end"
"0"
"1"
"2"
...

Apple says enumerateObjectsWithOptions:usingBlock: is synchronous, so "end" should not be printed before the enumeration, right?

Can you guys confirm?

like image 251
Duck Avatar asked Sep 19 '26 05:09

Duck


1 Answers

enumerateObjectsUsingBlock: is definitely synchronous. I just ran the same example in CodeRunner:

NSArray *myArray = @[ @1, @2, @3, @4, @5 ];
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%d", idx);
}];

NSLog(@"end");

And got the following output:

0
1
2
3 
4
end
like image 134
James Frost Avatar answered Sep 20 '26 21:09

James Frost