Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

27. Blocks > Basics of Using Blocks - Pg. 489

Basics of Using Blocks uses them. A block variable can declare the names of its arguments, but those names are irrelevant in the block itself. So, here are some legal block and block variable pairings: void (^foo)(int) = ^void(int x) { NSLog(@"%d", x); }; // Since the block literal calls its argument x, it doesn't matter // what the block variable calls it. void (^foo)(int y) = ^void(int x) { NSLog(@"%d", x); }; This block and block variable pairing, on the other hand, would cause an error. // This is a compiler error because the block calls its argument x, but it references // the variable y, which doesn't exist void (^foo)(int y) = ^void(int x) { NSLog(@"%d", y); }; Furthermore, the type of a block variable must match the type of the block it points to. If not, the compiler will give you an error. Here are some examples of erroneous block and block variable pairings: // This is a compiler error because the return types differ (void vs. int)