As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
As a property:
@property (nonatomic, copy) 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) {...};
4.块的内存管理:
4.1块其实是一个Objective-C类。块可以调用 copy, release, autorelease, 或者Block_copy(); Block_release(),来管理引用计数, 块是基于栈的,所以retain操作不起作用,必须用copy复制块(这有时候也是陷阱,不过我都没用过这么高级的东东,所以,初学者也太担心这个)。
4.2一个块在被复制的时候,它所引用的变量如果保存一个对象,那么这个对象会被保留(retain), 这个块释放时,也会释放这个对象(release).
4.3 块内调用的对于self的直接引用会导致self被保留(retain), 直接引用实例对象的变量,也会导致self 被保留(retain),方法中的本地变量,也会被保留(retain)。