I\'m trying to understand blocks. I get how to use them normally, when passed directly to a method. I\'m interested now in taking a block, storing it (say) in an instance va
Copy a block when you want it to stay around. Autorelease or release it when you're through with it. Retain it if you need a long way to spell /* NOP */
.
@interface Foo : FooSuper {}
@property(copy) int (^storedBlock)(int);
@end
@implementation Foo
@synthesize storedBlock = mStoredBlock;
- (void)setupStoredBlock {
self.storedBlock = ^{/*...*/};
// or: mStoredBlock = [^{/*...*/} copy];
// but this simple implementation violates the atomicity contract
}
- (void)runStoredBlock {
int result = self.storedBlock(5);
NSLog(@"%s: result = %d", __func__, result);
}
@end