__block directive, a simple explanation

Nowadays, if you pass a variable inside a block and try to assign a new value to it, you will encounter the error Variable is not assignable (missing __block type specifier).

This is a definitive improvement from the previous behavior where your variables simply remained unmodified. Now consider the following example:

    NSMutableString *digits = [NSMutableString stringWithString:@"12345"];

    void (^someBlock) (void) = ^{
        [digits appendString:@"67890"];

    };

    someBlock();

    NSLog(@"%@", digits);

Since digits was modified inside a block without the __block directive, you could think it will print the sequence “12345”.

Surprisingly, digits actually holds the value “1234567890”.

Why?

The answer is simple. When you make use of a variable inside a block. It is imported into the block structure as a const. Hence, the behavior is consistent with the declaration:

NSMutableString * const importedDigits = digits;

You would not be able to point importedDigits to a different address, but nothing stops you from changing the content of the object digits being referenced.

By using the __block directive, the variable is no longer imported to the block. Instead, it’s passed by reference and its behavior will be consistent with the declaration:

NSMutableString **digitsReference = &digits;

In this case, you can move where digitsReference is pointing at and you will actually be affecting the content of the variable digits.

For more information on the topic, check out the Block Implementation Specification in Clang’s documentation.

Optimizing NSPredicates

This week I’m gonna throw three strategies you can use to improve the performance of your NSPredicates — either in Core Data or while filtering a collection like NSArray.

If things are really slow though, there are probably other places where you should be focusing, but let’s assume you have already done that. 😉

Avoid Wildcards

The convenience of throwing a star wildcard (*) to look for a prefix, suffix or substring can make your predicates slower. Instead of using title LIKE "The *" use

title BEGINSWITH "The"

Instead of term LIKE *someSuffix" use

term ENDSWITH "someSuffix"

If you are looking for the occurrence of a substring within a string, don’t use term MATCHES '*anything*', there is a super cool keyword exactly for that:

term  CONTAINS "anything"

Cheap Computations First

Unlike you, your iPhone’s little brain is faster crunching math than processing text.

Perform the operations with numbers first (this includes NSDate) and leave the NSString comparisons at the end of the predicate.

Discard largest groups first

Place the comparisons that will eliminate the largest group of data at the beginning of the predicate. This leaves a smaller number of objects for the next comparisons (which should be the most expensive comparisons).

Game of Life Cellular Automata

For this week I want to share a concept and an example repository of something called Cellular Automata.

To be honest, it’s one of those simple things with a big name. Let me explain:

Imagine a grid of cells. Each cell has a finite number of states (let’s say 0 or 1 at its most basic case).
The system has an initial configuration for each cell (input) and changes over time according to some fixed rules. These rules determine the new state of each cell in terms of its current state and the state of the cells surrounding it (called neighbors). Finally, a particular state in time of the grid is called generation.

And this is how a two dimensional cellular automata looks (know that they can happen in any number of dimensions):

Animation

Indeed! With some imagination they do look like cells partying like it’s Friday!

This particular example is called ‘Game of Life’. It is a popular cellular automaton proposed by John Conway in 1970 and, using two basic rules, can generate groups of cells with lots of different behaviors.

If you want to see how the animation was coded, check out the code of the repository (iOS application) or if you are interested in the topic, I suggest the first chapter of the book ‘Game of Life Cellular Automata’ by Andrew Adamatzky.

As usual, this repository is licensed under the MIT License.