Last Updated: February 25, 2016
·
428
· yuyak

How to Write Objective-C Blocks

Since there are some ways to write Objective-C blocks, here are some snippets not to forget them.

Without artuments:

void (^block1)() = ^{
};
block1();

With some arguments:

NSInteger (^block2)(NSInteger, NSObject *) = ^(NSInteger arg1, NSObject *arg2) {
    return arg1;
};
block2(0, nil);

With typedef

typedef void (^Block3)(id, NSError *);
typedef void (^Block4)(id response, NSError *error);
Block4 block4 = ^(id response, NSError *error) { };

With @property

@interface Object : NSObject

@property (nonatomic, copy) void (^block5)(id, NSError *);

@end