NSMutableArray enumerateObjectsUsingBlock is not synchronous as Apple says

Deadly 提交于 2019-12-13 14:51:28

问题


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?


回答1:


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



回答2:


Cannot confirm this, it's printing as expected: "0","1","2","End". Also notice that you are printing indexes but your log starts at "1".



来源:https://stackoverflow.com/questions/21256372/nsmutablearray-enumerateobjectsusingblock-is-not-synchronous-as-apple-says

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!