Blocks on Blocks on Blocks

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.

Syntax

Blocks start off with the ^ symbol. Like functions, they also need an open and close bracket.

Variable Assignment

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 myBlock().

Check back for follow up posts about using blocks with arguments and block scope.

About these ads
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s