修改捕获的变量

Block 将捕获出现在相同词法范围内的变量。通常这些变量被捕获为 const 值:

int val = 10;
void (^blk)(void) = ^{
    val = 20; // Error! val is a constant value and cannot be modified!
};

要修改变量,需要使用__block 存储类型修饰符。

__block int val = 10;
void (^blk)(void) = ^{
    val = 20; // Correct! val now can be modified as an ordinary variable.
};