Blocks

Blocks are an extension to the C language that were first supported in iOS 4. They are similar in concept to a method or function that can be stored in a variable. C provides a similar mechanism for storing functions: function pointers. Blocks go beyond function pointers by storing a copy of the enclosing scope in addition to the block of executable code.

When a block is defined, a copy of the local stack is created and attached to the block. When the block is finally executed, it has access to this copy of the stack. This is very powerful, allowing a block of code and its surrounding state to be passed to a method. This code can be executed at a future point or within a certain context, such as on another thread or in a certain order. ...

Get The Core iOS Developer’s Cookbook, Fifth Edition 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.