修改捕獲的變數

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.
};