我意识到我上面的评论令人难以置信的混乱。希望以下内容能澄清我想说的话:
- (void)yourMethod:(Foo *)parameterFoo
{
__block Foo *blockVariable = [Foo someFoo];
Foo *capturedVariable = [Foo anotherFoo];
void(^doWorkBlock)(Foo *bp) = ^(Foo *bp){
// If your block accesses a scoped variable that is not marked with __block, it will
// retain it, so here capturedVariable and bp would be retained by the block
capturedVariable.aProperty = 5.0;
bp.aProperty = 10.0;
// As you can see, you can modify the objects all you like.
// What you cannot do is assign something to capturedVariable or bp because they
// were not marked as __block
// WONT WORK
capturedVariable = [Foo new];
// However, you can write to blockVariable because it is marked as __block
// WORKS
blockVariable = [Foo new];
// Remember, though that the block will not retain this variable itself because
// of the __block
};
// Note, it's weird for the block to take a parameter since it could just access the
// variable directly. This just serves to show how a block would handle a parameter.
doWorkBlock(parameterFoo);
}