August 2011
Intermediate to advanced
552 pages
23h 48m
English
You need to copy a block before you add it to a Cocoa collection class. Consider this erroneous code:
NSMutableArray *blockArray = [NSMutableArray array];
[blockArray addObject: ^{
NSLog (@"Jen makes great cupcakes!");
}];
The stack-based block object has its address passed to -addObject:. -addObject: retains the passed-in object. Because the block is still on the stack, -retain is a no-op. The block becomes invalid when the calling function exits, waiting to blow up when you use it in the future. You fix this by copying it before putting it into the collection. Do not forget to offset ...
Read now
Unlock full access