## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 6.1. Constructing Block Objects

## Problem

You want to be able to write your own block objects or use block objects with iOS SDK classes.

## Solution

You just need to understand the basic differences between the syntax of block objects and classic C functions. These differences are explained in the Discussion section.

## Discussion

Block objects can either be inline or coded as independent blocks of code. Let’s start with the latter type. Suppose you have a method in Objective-C that accepts two integer values of type `NSInteger` and returns the difference of the two values, by subtracting one from the other, as an `NSInteger`:

````-` `(``NSInteger``)` `subtract:``(``NSInteger``)``paramValue`
`from:``(``NSInteger``)``paramFrom``{`

`return` `paramFrom` `-` `paramValue``;`

`}````

That was very simple, wasn’t it? Now let’s translate this Objective-C code to a pure C function that provides the same functionality to get one step closer to learning the syntax of block objects:

````NSInteger` `subtract``(``NSInteger` `paramValue``,` `NSInteger` `paramFrom``){`

`return` `paramFrom` `-` `paramValue``;`

`}````

You can see that the C function is quite different in syntax from its Objective-C counterpart. Now let’s have a look at how we could code the same function as a block object:

````NSInteger` `(``^``subtract``)(``NSInteger``,` `NSInteger``)` `=`
`^``(``NSInteger` `paramValue``,` `NSInteger` `paramFrom``){`

`return` `paramFrom` `-` `paramValue``;`

`};````

Before I go into details about the syntax of block objects, let me show you a few more examples. Suppose we have a function in C that takes a parameter of type `NSUInteger` (an unsigned integer) ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required