Mac OS 10.6 Was implemented in gcc 4.2 (with bugs) and clang 3.1 First blocks-oriented API is the Grand Central Dispatch iOS 5 ARC (changed __block semantics) iOS 7 no need to call copy for blocks in many cases
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) {...}; http://fuckingblocksyntax.com
objects compatible with id Have *isa as first member Managed with retain/release/copy Can be passed as parameters and retuned from functions and methods Can be stored in containers Can be used in C++ algorithms as functors
and prolong their lifetime By default values are captured as const, pointer to objects are referenced so using of self in block creates retain cycle if block somehow stored in the object __block changes semantics of capturing (different with and without ARC)
block Access to ivar is done throw implicit self ^ { NSLog(@"%@", _ivar); }; ^ { NSLog(@"%@", self->_ivar); }; Retain cycle when block is saved as class member
[weakSelf doOtherThing]; }]; __weak typeof(self) weakSelf = self; [obj doWithBlock:^ { __strong typeof(self) strongSelf = weakSelf; [strongSelf doThing]; [strongSelf doOtherThing]; }]; self still can be used and create retain cycle (for example when using RACObserve)
the block is called, strong during call self should called self Checking for ivar access in the block [RACObserve(self, pttState) subscribeNext:@weakselfnotnil(^(NSNumber *state)) { self.isRecordingPTT = !!state.intValue; } @weakselfend];