It’s time to learn about blocks. I first heard about blocks when I began my journey to become a proficient iOS developer. At that time, I thought blocks were unique to Objective C, but that is not the case. Blocks can also be used in C and C++.
So what is a block? A block is a language feature that can be thought of as representing a task. You might think that sounds like a simple method and it does, but the difference between blocks and functions is that blocks are Objective-C objects. Since they are objects they can be passed around as such and put into data structures.
Blocks start off with the
^ symbol. Like functions, they also need an open and close bracket.
You can assign a variable to keep track of your block. This works in the same way that a function pointer in C works. Since this is a variable assignment statement, it must end in a
;. In the Gist above our variable is called
myBlock and you can call the block simply by typing
Check back for follow up posts about using blocks with arguments and block scope.