Using the weakSelf/strongSelf
pattern to avoid creating a retain cycle in blocks, this code is pretty common:
typeof(self) __weak weakSelf = self;
void (^block)() = ^{
typeof(weakSelf) strongSelf = weakSelf;
// ...more code...
};
The question is, does changing the second typeof(weakSelf)
to typeof(self)
cause self
to be captured in the block?
For example:
typeof(self) __weak weakSelf = self;
void (^block)() = ^{
typeof(self) strongSelf = weakSelf; // does using typeof(self) here end up capturing self?
// ...more code...
};
If self is not captured, is there any reason to prefer one way or the other?
It shouldn't. If it does, it's a compiler bug.
The typeof
expression doesn't actually reference the variable self
or its value. It's strictly a reference to the expression's type, not its value. The expression is strictly a compile-time construct which doesn't survive into the compiled code.
I would prefer typeof(self)
, personally, but I don't think there's a strong argument to prefer one or the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With