標籤:
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)。
How Do I Declare A Block in Objective-C?