In the code below, self is retained to assure that the image object lives when the block gets called. That’s what the docs say. However, I don’t seem to understand why. Simply retaining the image would have guarantee that it doesn’t get deallocated. So why retain self as well?
self.finishBlock = ^{
self.image.hidden = YES;
}
Does this apply if you access the image directly?
self.finishBlock = ^{
_image.hidden = YES;
}
A block needs to retain any captured objects in the block. Your first block example is really:
The block must retain
selfso it can properly call theimagemethod. As written the block can’t simply retainimagebecause the image isn’t obtained until the block is executed and theimagemethod is called. So the only option here is to retainself.In the second block you really have:
so again,
selfmust be retained so the proper value of the_imageivar is accessed when the block is actually executed.