Coding Standards: Fast enumeration is not the only way

Lately I’ve been experimenting more and more with blocks in Objective-C: I started writing much more block-oriented code, integrated blocks into existing classes that I’ve written and I generally find myself thinking about how to make code more efficient, readable and bug-free using blocks.

As part of this blocky transformation that I’m going through, I have discovered some very convenient ways to enumerate collections (namely NSSet, NSArray and NSDictionary). These classes have built-in instance methods that allow you to traverse their contents in a very convenient, block-based way.

Examples please…?

Here are a couple of good examples for using block based enumeration:


    NSArray *myBeers = @[@"Hoegaarden",@"Leffe",@"Guiness",@"Corona"];
    [myBeers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *beerName = obj;
        NSLog(@"Beer #%d is %@",idx,beerName);
        if ([beerName isEqualToString:@"Guiness"]) {
            //Assigning YES to the stop variable is the equivalent of calling "break" during fast enumeration
            *stop = YES;


    NSDictionary *countriesToBeers = @{@"Belgium" : @"Duvel",
                                       @"Ireland" : @"Guiness",
                                       @"Mexico" : @"Dos Equis"};
    [countriesToBeers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSString *country = key;
        NSString *beer = obj;
        NSLog(@"The best beer in %@ is %@. No doubt.",country,beer);
But why blocks??

But why blocks?? I love fast enumeration!

Well… I’m not saying that fast enumeration is always bad, but block enumeration has it’s advantages:
1. It’s a bit faster. And I say “a bit” because I’m demonstrating this principal using very simple examples. Once enumeration is done on  more complex structures, the elimination of the need for using NSFastEnumeration’s overhead results in a more significant speedup.
2. It gives you a bonus. For NSArrays, you gain a free index variable you can use, no extra charge. For NSDictionaries you get a reference for the object’s key, as well as the object itself without any need for extra code. This means that you can traverse a dictionary’s overall contents and not only it’s values or keys separately.

Hope this has opened your mind to better coding standards. Let the weekend begin!


Posted on December 21, 2012, in Coding standards and tagged , , , , . Bookmark the permalink. 3 Comments.

  1. It’s really opened mind , i have used yet but from today onwards i am sure gonna use it. Thank You for Wonder ful blog.

  2. It’s really opened mind , i have not used yet but from today onwards i am sure gonna use it. Thank You for Wonder ful blog.

  3. Nice, I was looking for a concise article on this. This one hit the spot!

Say something...

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

%d bloggers like this: