Building Blocks – Objective C

abcYou can only work for so long in the world of Objective C before you become exposed to the concept of ‘Blocks’. A very powerful tool in the arsenal of any developer with which they can drastically increase the amount of code reuse that a developer can leverage in their program. Code reuse is of course a great thing as tried and trusted code can be relied upon to perform correctly and will need the minimum amount of testing whereas duplicated code tends to, as a former colleague of mine used to remark,  lead to the worst kind of inheritance; that is :-  ‘clipboard inheritance’ responsible for many a brainteasing bug. I have also seen it reported that Blocks effectively give you ‘lambda’ in Objective C which as a heavyweight user of LINQ in my preferred language C# I must admit makes me feel a little easier about taking on more Objective C work. They are however a little bit tricky to get your head around when you first come to dealing with them.

So on with building our blocks. In this example we have a function which will take an array of projects and will return them grouped by a particular key ( that the user may change) to allow them to be displayed in a MasterView. Note that I am not for the benefits of this example interested in the graphical implementation side of this only the supporting code that makes this possible.

So Let us start with the main ‘Hub’ function which deals with most of the work we are going to throw at our function.


OK so line by line as you can see we have defined a function called ‘groupByHub’ which returns an NSMutableArray object. This in turn will contain NSMutableArrays containing projects grouped by the selected category. More on that later… The function takes two arguments:-

1. source being all of the projects our application can see.

2. groupFunc being the ‘block’ or piece of code that we are going to call in order to perform the grouping.

+(NSMutableArray*)groupByHub:(NSMutableArray *)source groupFunc:(groupByValue)groupFunc{

Ok So on to the following line we then create a NSMutableDictionary which is great for sorting our data out (although not so great for using in the UI layer I believe).

NSMutableDictionary *groupedData=[[NSMutableDictionary alloc] init];

The next line fairly simply iterates over all of the projects within our collection of projects.

for(Project *item in source){

We then use our ‘Block’ to obtain the value of the category pertaining to this particular project. We will examine the blocks themselves in a little while  

NSString *groupName = groupFunc(item);

The next few lines then deal with adding the project item to the relevant array within the dictionary. We see if an array for the current Group Name has been defined. If not we will create a new one and add it to the dictionary otherwise we will use the one already created. We will then add the ‘project’ item to the array we found/created. The Code for that looks like this:-

NSMutableArray *arr= [groupedData objectForKey:groupName];
if(arr == nil){
     arr = [[NSMutableArray alloc]init]; 
     [groupedData setObject:arr forKey:groupName];
[arr addObject:item];

We then iterate over all of the elements added to the dictionary and add them to the array that we wish to return. 

NSMutableArray *array = [[NSMutableArray alloc]init];
for(NSMutableArray *ar in groupedData.objectEnumerator){
     [array addObject:ar];
return array;

OK. SO that’s the meat and veg of our main function, it doesn’t however describe how the blocks are implemented so without further ado here is the main .h file:-


So defined below is the type defintion for our block, a very simple block by most standards but that is the point of examples! The name of the block is groupByValue, tt returns a string (the category value) and accepts a project item (which it will interrogate to ascertain the return value): 

typedef NSString* (^groupByValue)(Project *);

Note that we also define three other functions which will all in turn call the main groupByHub function passing across the ‘block ‘ to use to return data grouped in the appropriate manner. So now to the implementation:-


We only really need to examine the code for our GroupByCustomerName function to see what is going on here. We start off by by defining the implementation of our block. The type name ‘groupByValue’ matches our typedef from the .h file as does the signature. The only real difference is that is this is an implementation we use a variable to refer to the block later in code. In this instance we use the variable name ‘gp’. The code within the block is extremely simple, given a project that is passed in we return the value of this group, in this instance ‘Customer Name’ but in other instances we also  return ‘Project Manager’ and ‘Account Manager’.
groupByValue gp=^(Project *item){
return item.CustomerName;

All that is then left is to call our main ‘groupByHub’ function passing across the data source and of course the block that we will use to perform the grouping

return [self groupByHub:source groupFunc:gp];

And Voila, There You go, Blocks… As easy as A,B and C.

As an aside, whilst I have tons of experience in C# I am a relatively inexperienced in the world of Objective C. Whilst I know that I will get there if there are any experienced Objective C coders who see omissions, mistakes or just better methodologies I would welcome your constructive criticism.


3 thoughts on “Building Blocks – Objective C

  1. I am extremely impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you modify it yourself?
    Anyway keep up the excellent quality writing, it is rare to see a great blog
    like this one today.

  2. Pingback: Homepage

Leave a Reply

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

You are commenting using your 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