in GCD, Programming

How to declare a block

Are you really sure on how to declare a block? How about all 4 variations? On iOS and macOS, it can be easy to forget Objective-C block syntax as it isn’t the most intuitive, especially now Swift is becoming the main language of new projects. Remembering the syntax is actually just a short click away but here are easy-to-read example uses with nullability tips and things to watch out for when declaring a block.

Examples

Declaring a block as a property called completion on an object:

Declaring a method that accepts a block called completion:

Declaring a block for use in an argument to the method defined above:

Declaring a block as a local variable called numberCrunch:

Nullability

If you’re not using nullability in your Objective-C project, just strip out the keywords otherwise use the underscore versions _Nonnull and _Nullable with the block pointers. If you’re declaring a block as a property on an object you can use the nullable and nonnull versions.

Retaining

It is worth remembering blocks capture and retain the objects used inside them and copy the values of primitive types (int, float, etc). You can release them by releasing the block (either through nilling the property or letting it leave the scope if it is an instance variable).

If you access the object the block is a property of i.e. by calling self in the block, then as the object retains the block… the block will retain any referenced objects. This creates a retain-cycle so the self object would never get released. This only happens when the block is a property of the self object but it is considered good practice to use a weak variable to reference self to avoid this situation. You can also enable a build setting for LLVM in your Xcode Project to flag “Implicit retain of ‘self’ within blocks” as a warning.

That is everything for now. I’ll be posting some advanced tips and tricks for using blocks later including: –

  • Building a block completion queue
  • Things to watch when using blocks in UI views
  • Using a typedef for blocks to simplify protocols dramatically

Write a Comment

Comment