How Do I Declare A Block in Objective-C?
http://fuckingblocksyntax.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 (^nullability)(parameterTypes))blockName;As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters)
{...}];As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
This site is not intended to be an exhaustive list of all possible uses of blocks.
If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.
If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.
本文详细介绍了如何在 Objective-C 中声明和使用 Blocks。包括作为局部变量、属性、方法参数、方法调用参数以及类型定义等不同场景下的 Blocks 语法。
822

被折叠的 条评论
为什么被折叠?



