Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [77]

By Root 443 0

@"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.

Declaring a block variable

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.

Figure 32.1 Block variable declaration

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.”

Assigning a block

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.

Passing in a block

Because devowelizer is a variable, you can pass it as an argument.

Return Main Page Previous Page Next Page

®Online Book Reader