Graphics Programs Reference
In-Depth Information
block it will point to. Because there are more components in a block variable's type, de-
claring a block variable requires a different syntax:
int (^adder)(int a, int, b);
This is what a block variable looks like. The name of this variable is adder , and it will
point to a block that takes two int arguments and returns an int . The caret symbol ( ^ )
is what distinguishes the variable as a pointer to a block - just like an asterisk (*) indicates
that a variable is a pointer to an object. Here are more examples of block variables that
point to different types of blocks:
// A block variable named foo; it can point to a block that takes no arguments
// and returns no value:
void (^foo)(void);
// A block variable named jump; it can point to a block that takes one Hurdle *
// and returns no value:
void (^jump)(Hurdle *);
// A block variable named doubler; it can point to a block that takes one int
// and returns an int:
int (^doubler)(int);
Notice that the syntax is like declaring a C function except that, instead of a function
name, there is a variable name prefixed with a caret and wrapped in parentheses.
In BNRAppDelegate.m , add a local block variable to applica-
tion:didFinishLaunchingWithOptions: .
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
int (^adder)(int, int);
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
This declares a variable named adder that will point to a block that returns an int and
takes two int s as arguments. Build the application. You should see a single warning say-
ing that the variable adder isn't used. (If you see anything else, check your syntax and
build again.)
Search WWH ::




Custom Search