To see how this works, we’re going to dive right into some code. In this exercise, you’re going to use a block to remove all of the vowels from each of the strings in an array.
Create a new Foundation Command Line Tool and call it VowelMovement. In this program, you will use a block to iterate over an array of strings, transforming each one. First, you’re going to create three arrays: one to store the original strings, one to store the “devowelized” strings, and a third to store the characters to strip from the strings. In main.m, replace the code inside of the @autoreleasepool’s curly braces:
int main (int argc, const char * argv[]) { @autoreleasepool { // Create the array of strings to devowelize and a container for new ones NSArray *oldStrings = [NSArray arrayWithObjects: @"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil]; NSLog(@"old strings: %@", oldStrings); NSMutableArray *newStrings = [NSMutableArray array]; // Create a list of characters that we'll remove from the string NSArray *vowels = [NSArray arrayWithObjects: @"a", @"e", @"i", @"o", @"u", nil]; } return 0; }
Nothing new here; you’re just setting up arrays. Build and run your program. You can ignore the warnings about unused variables for now.
Now comes the code for the block. Although blocks look like functions, they can be stored in variables. Like other variables, block variables are declared and then assigned values. Add the following code to main.m to declare your block variable.
int main (int argc, const char * argv[]) { @autoreleasepool { // Create the array of strings to devowelize and a container for new ones NSArray *oldStrings = [NSArray arrayWithObjects: @"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil]; NSLog(@"old strings: %@", oldStrings); NSMutableArray *newStrings = [NSMutableArray array]; // Create a list of characters that we'll remove from the string NSArray *vowels = [NSArray arrayWithObjects: @"a", @"e", @"i", @"o", @"u", nil]; // Declare the block variable void (^devowelizer)(id, NSUInteger, BOOL *); } return 0; }
Let’s break down this declaration to see what’s going on.
When declaring a primitive variable, you give its type and then its name, like int i. For a block variable, however, the name is in the middle of the declaration right after the caret. The type of the block variable is dependent on how the block is constructed. In this case, devowelizer’s type is “a block that takes an object, an integer, and a BOOL pointer, and returns nothing.”
Now let’s assign a value to our new variable. For a block variable, the value is always a set of instructions inside curly braces. In main.m, add the following assignment:
int main (int argc, const char * argv[]) { @autoreleasepool { // Create the array of strings to devowelize and a container for new ones NSArray *oldStrings = [NSArray arrayWithObjects: @"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil]; NSLog(@"old strings: %@", oldStrings); NSMutableArray *newStrings = [NSMutableArray array]; // Create a list of characters that we'll remove from the string NSArray *vowels = [NSArray arrayWithObjects: @"a", @"e", @"i", @"o", @"u", nil]; // Declare the block variable void (^devowelizer)(id, NSUInteger, BOOL *); // Assign a block to the variable devowelizer = ^(id string, NSUInteger i, BOOL *stop) { NSMutableString *newString = [NSMutableString stringWithString:string]; // Iterate over the array of vowels, replacing occurrences of each // with an empty string. for (NSString *s in vowels) { NSRange fullRange = NSMakeRange(0, [newString length]); [newString replaceOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange]; } [newStrings addObject:newString]; }; // End of block assignment } return 0; }
Build your program again to check your typing. The warnings about unused variables should disappear.
Now you’ve composed a block – a set of instructions – and assigned the block to the block variable devowelizer. Notice that the block assignment ends with a semi-colon just like any variable assignment would.
As with any variable, you can perform the declaration and assignment of devowelizer together:
void (^devowelizer)(id, NSUInteger, BOOL *) = ^(id string, NSUInteger i, BOOL *stop) { NSMutableString *newString = [NSMutableString stringWithString:string]; // Iterate over the array of vowels, replacing occurrences of each // with an empty string. for (NSString *s in vowels) { NSRange fullRange = NSMakeRange(0, [newString length]); [newString replaceOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange]; } [newStrings addObject:newString]; };
Just like before, here we declare a block variable that takes three arguments, returns nothing, and is called devowelizer. Then we compose an actual block and store it in devowelizer.
Because devowelizer is a variable, you can pass it as an argument. NSArray has a method called enumerateObjectsUsingBlock:. This method expects a block as its sole argument. It will execute that block once for each object in the array.
In main.m, add the following code to call enumerateObjectsUsingBlock: with devowelizer and then print out the devowelized strings.
int main (int argc, const char * argv[]) { @autoreleasepool { // Create the array of strings to devowelize and a container for new ones NSArray *oldStrings = [NSArray arrayWithObjects: @"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil]; NSLog(@"old strings: %@", oldStrings); NSMutableArray *newStrings = [NSMutableArray array]; // Create a list of characters that we'll remove from the string NSArray *vowels = [NSArray arrayWithObjects: @"a", @"e", @"i", @"o", @"u", nil]; // Declare the block variable void (^devowelizer)(id, NSUInteger, BOOL *); // Assign a block to the variable devowelizer = ^(id string, NSUInteger i, BOOL *stop) { NSMutableString *newString = [NSMutableString stringWithString:string]; // Iterate over the array of vowels, replacing occurrences of each // with an empty string. for (NSString *s in vowels) { NSRange fullRange = NSMakeRange(0, [newString length]); [newString replaceOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange]; } [newStrings addObject:newString]; }; // End of block assignment // Iterate over the array with our block [oldStrings enumerateObjectsUsingBlock:devowelizer]; NSLog(@"new strings: %@", newStrings); } return 0; }
Build and run your program. You’ll see two arrays logged to the console. The second array should match the first array – without all those pesky vowels.
2011-09-03 10:27:02.617 VowelMovement[787:707] old strings: ( Sauerkraut, Raygun, "Big Nerd Ranch", Mississippi ) 2011-09-03 10:27:02.618 VowelMovement[787:707] new strings: ( Srkrt, Rygn, "Bg Nrd Rnch", Msssspp )
It’s important to note that enumerateObjectsUsingBlock: won’t accept just any block as its argument. This method requires “a block that takes an object, an integer, and a BOOL pointer and returns nothing.” That’s why we constructed the block that we assigned to devowelizer as we did. Its three arguments are specifically designed for going through an array.
The first argument is a pointer to the current object. Notice that this pointer’s type is id so that it will work no matter what kind of objects the array contains. The second argument is an NSUInteger that is the index of the current object. The third object is a pointer to a BOOL, which defaults to NO. We can change it to YES if we want the array to stop after the current iteration.
Add a check at the beginning of the block assignment:
devowelizer = ^(id string, NSUInteger i, BOOL *stop){ NSRange yRange = [string rangeOfString:@"y" options:NSCaseInsensitiveSearch]; // Did I find a y? if (yRange.location != NSNotFound) { *stop = YES; // Prevent further iterations return; // Stop this iteration } NSMutableString *newString = [NSMutableString stringWithString:string]; // Iterate over the array of vowels, replacing occurrences of each // with an empty string. for (NSString *s in vowels) { NSRange fullRange = NSMakeRange(0, [newString length]); [newString replaceOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange]; } [newStrings addObject:newString]; }; // End of block assignment
This will check to see if the string for the current iteration contains an uppercase or lowercase ‘y’ character. If it does, the pointer is set to YES (which will prevent the block from performing any more iterations) and then the current iteration is halted.
Build and run the program. Again, two arrays are logged to the debugger output, but this time, the array enumeration was cancelled during the second iteration when the block encountered a word with the letter ‘y’ in it. All you get is Srkrt.
Now that you’ve had some practice with blocks, let’s return to how blocks help solve the problem of far-flung code bits in your programs. When you use a callback, such as in Chapter 24 where you had this line of code:
[[NSNotificationCenter defaultCenter] addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
you identify a method (typically using @selector()), and then you implement that method somewhere else in the file:
- (void)zoneChange:(NSNotification *)note { NSLog(@"The system time zone has changed!"); }
You could use the NSNotificationCenter method addObserverForName:object:queue:usingBlock: and pass a block instead. With this method, you hand the NSNotificationCenter the instructions right then, so you don’t have to put the code for the callback somewhere else. Anyone reading your code will see the instructions and the message send to the NSNotificationCenter in the same chunk of code. (You’ll get to make exactly this change to your Callbacks program in a challenge at the end of the chapter.)