Objective-C pass block as parameter Objective-C pass block as parameter objective-c objective-c

Objective-C pass block as parameter


The type of a block varies depending on its arguments and its return type. In the general case, block types are declared the same way function pointer types are, but replacing the * with a ^. One way to pass a block to a method is as follows:

- (void)iterateWidgets:(void (^)(id, int))iteratorBlock;

But as you can see, that's messy. You can instead use a typedef to make block types cleaner:

typedef void (^ IteratorBlock)(id, int);

And then pass that block to a method like so:

- (void)iterateWidgets:(IteratorBlock)iteratorBlock;


The easiest explanation for this question is follow these templates:

1. Block as a method parameter

Template

- (void)aMethodWithBlock:(returnType (^)(parameters))blockName {        // your code}

Example

-(void) saveWithCompletionBlock: (void (^)(NSArray *elements, NSError *error))completionBlock{        // your code}

Other use of cases:

2. Block as a Property

Template

@property (nonatomic, copy) returnType (^blockName)(parameters);

Example

@property (nonatomic,copy)void (^completionBlock)(NSArray *array, NSError *error);

3. Block as a method argument

Template

[anObject aMethodWithBlock: ^returnType (parameters) {    // your code}];

Example

[self saveWithCompletionBlock:^(NSArray *array, NSError *error) {    // your code}];

4. Block as a local variable

Template

returnType (^blockName)(parameters) = ^returnType(parameters) {    // your code};

Example

void (^completionBlock) (NSArray *array, NSError *error) = ^void(NSArray *array, NSError *error){    // your code};

5. Block as a typedef

Template

typedef returnType (^typeName)(parameters);typeName blockName = ^(parameters) {    // your code}

Example

typedef void(^completionBlock)(NSArray *array, NSError *error);completionBlock didComplete = ^(NSArray *array, NSError *error){    // your code};


This might be helpful:

- (void)someFunc:(void(^)(void))someBlock;