Variable Capturing

A block can use any variables that are visible within its enclosing scope. The enclosing scope of a block is the scope of the method in which it is defined. Thus, a block has access to all of the local variables of the method, arguments passed to the method, and instance variables that belong to the object running the method. In the actionBlock code above, both the BNRItem (item) and the BNRItemCell (cell) have been captured from the enclosing scope.

Blocks own the objects that they capture, and this can easily result in a strong reference cycle. Take a look back at the warning you had when creating rect within the actionBlock: Capturing ‘cell’ strongly in this block is likely to lead to a strong reference cycle ...

Get iOS 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.