DevPrep

All topics › Objective-C

How do blocks work and why is __block needed?

Senior PRO

Essence

A block (^{ ... }) is a closure in Objective-C: an object (its isa is one of the block classes) that contains a function pointer and copies of the captured variables. A block can be stored, passed around and called later.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store