Typedef Blocks

It is a good practice to typdef blocks before using them in Objective-C. Doing so will not only make our code more readable, but will allow us to tweak the process by which we get the result in one place and make sure the update effects every single outcome where the block was called. Here is how we would typedef a block:

The block defined above is called “operation_t” (you can use whatever name you want), with return type “int”, and an argument “op” of type “int” as well.

After defining our block, we will have to implement it:

Here is how we would use it in our code:

You can always NSLog the result to the console to make sure it works:

There is a way to use blocks without typedef(ing) them in our header or implementation file, but I choose not to do it for the reasons I’ve mentioned earlier.

Typedef Blocks