Objective-C Block

OC中block的语法实在是太别扭了,不知道是不是我太孤陋寡闻了。。。在此记录一下,以供日后查找使用。

引用:http://goshdarnblocksyntax.com

As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

As a property:
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);

As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};

License: CC BY-SA 4.0

Contact