How To Use Blocks in iOS 5 Tutorial – Part 2

This is a blog post by iOS Tutorial Team member Adam Burkepile, a full-time Software Consultant and independent iOS developer. Check out his latest app Pocket No Agenda, or follow him on Twitter. Welcome back to our tutorial series on using blocks in iOS 5 – with some Storyboard/Interface Builder practice along the way! In […] By .

Leave a rating/review
Save for later
Share
You are currently viewing page 6 of 6 of this article. Click here to view the first page.

Blocks and Autocompletion

One final tip. When you're using a method that takes a block in Xcode, it can autocomplete the block for you, saving yourself time and syntax errors.

For example, type this into Xcode:

NSArray * array;
[array enum

At this point the autocompletion routine will find enumerateObjectsUsingBlock - hit enter to auto-complete that method name. Then hit enter again to auto-complete the block, and it will put in this:

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        code
    }

You can fill in your code and close out the method call, and viola - much easier than typing everything in!

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        // Do something
    }];

Where to Go From Here?

The final completed project code can be downloaded here. If you are familiar with git, I also have the project hosted here at github, with commits at each step if you get stuck at a step.

Hopefully in creating this simple app, you've been able to see the power and simplicity that Blocks add to your dev toolkit, and gotten some ideas of how you can use them in your own projects.

If this is your first exposure to blocks, you've taken a pretty big step. With more and more apps needing asynchronous, networked, and multi-threaded code, blocks will definitely be considered required learning.

There's still more to learn about blocks - specifically in terms of variable capture and the __block keyword. More information about blocks and Grand Central Dispatch can be found here:

Thanks for following along with me on my first (of hopefully many) tutorial for this site, and I hope to hear from you in the forums!


This is a blog post by iOS Tutorial Team member Adam Burkepile, a full-time Software Consultant and independent iOS developer. Check out his latest app Pocket No Agenda, or follow him on Twitter.