/syntaBlock

Objective-C blocking examples.

Apache License 2.0Apache-2.0

Objective-C Block Syntax

Basic

^{
	NSLog(@"This is a block");
}

Create and use

void (^simpleBlock)(void) = ^{
        NSLog(@"This is a block");
};
...
simpleBlock();

Defining via typedef

typedef returnType (^blockName)(parameterTypes);

Example: Objective-C:

typedef int (^stringToIntBlock)(NSString *);

Add returntype

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
Example
int (^calcSum)(int,int);
^(int value1, int value2) {
        return (value1 + value2);
}

Basic Creation and use

int (^calcSum)(int, int) = ^(int value1, int value2) {
								return value1 + value2;
							};
int total = calcSum(2, 5);
// total = 5