Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^
instead of *
. The block type fully interoperates with the rest of the C type system. The following are all valid block variable declarations:
void (^blockReturningVoidWithVoidArgument)(void); |
int (^blockReturningIntWithIntAndCharArguments)(int, char); |
void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int); |
Blocks also support variadic (...
) arguments. A block that takes no arguments must specify void
in the argument list.
Blocks are designed to be fully type safe by giving the compiler a full set of metadata to use to validate use of blocks, parameters passed to blocks, and assignment of the return value. You can cast a block reference to a pointer of arbitrary type and vice versa. You cannot, however, dereference a block reference via the pointer dereference operator (*
)—a block's size cannot be computed at compile time.
You can also create types for blocks—doing so is generally considered to be best practice when you use a block with a given signature in multiple places:
typedef float (^MyBlockType)(float, float); |
MyBlockType myFirstBlock = // ... ; |
MyBlockType mySecondBlock = // ... ; |
You use the ^
operator to indicate the beginning of a block literal expression and ;
to indicate the end of a block expression. The following example declares a simple block and assigns it to a previously declared variable (oneFrom
):
int (^oneFrom)(int); |
oneFrom = ^(int anInt) { |
return anInt - 1; |
}; |
If you don’t explicitly declare the return value of a block expression, it can be automatically inferred from the contents of the block. If the return type is inferred and the parameter list is void
, then you can omit the (void)
parameter list as well. If or when multiple return statements are present, they must exactly match (using casting if necessary).
At a file level, you can use a block as a global literal:
#import <stdio.h> |
int GlobalInt = 0; |
int (^getGlobalInt)(void) = ^{ return GlobalInt; }; |
Last updated: 2010-07-08