We can read auto var in block:
int aVar = 1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"theVar==%d", aVar);
});
But can not write:
int aVar = 1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
aVar = 2;
NSLog(@"theVar==%d", aVar);
});
Xcode showed:Variable is not assignable (missing __block type specifier)
.
I know that when there is no __block
,auto var pass to block as a copy so it's readonly.And with __block
its address pass to block.
But I can't understand why Apple must design as this?Just copy address to block is not OK?Is there any potential problem If auto var in block without __block
is writeable?