Variable Capture Redux
Let’s revisit variable capture. Each of the blocks here is the same code, but they are “created” at different times – each after the value of val has changed.
typedef void (^BoringBlock) (void); int val = 23; BoringBlock block1 = ^{ NSLog (@"%d", val); }; val = 42; BoringBlock block2 = ^{ NSLog (@"%d", val); }; val = 17; BoringBlock block3 = ^{ NSLog (@"%d", val); };
block1 points to a block that has captured val when it had a value of 23. block2 points to a block that has captured val when it had a value of 42, and likewise ...
Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.