我试图找出一种方法使用 typeof 创建一个弱引用 self 块,以避免保留周期。
当我第一次读到这个,似乎约定是使用 __ block typeof(self)bself = self; ,它使用 __ block 进行编译,以避免保留周期不再工作, __ weak
但是 __ weak typeof(self)bself = self; 会导致错误:
类型'typeof(self)'(也称为'TUAccountsViewController * const __strong')已经设置了保留属性 $ b
有没有办法使用 typeof 或另一个调用来通常创建一个弱引用 self
解决方案这样有效!
__ typeof __(o)__weak我在我的BBlock项目中定义了 BBlockWeakSelf 可以这样使用:
BBlockWeakSelf wself = self;github/kgn/BBlock/blob/master/BBlock.h
根据Aleph7编辑回应。
I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles.
When I first read about this it seems that the convention was to use __block typeof(self) bself = self;, which compiles but using __block to avoid retain cycles doesn't work anymore and __weak should be used instead.
However __weak typeof(self) bself = self; results in an error:
The type 'typeof (self)' (aka 'TUAccountsViewController *const __strong') already has retainment attributes set on it
Is there a way to use typeof or another call to generically create a weak reference to self?
解决方案This works!
__typeof__(o) __weakWhich I've defined in my BBlock project as BBlockWeakSelf which can be used like this:
BBlockWeakSelf wself = self;github/kgn/BBlock/blob/master/BBlock.h
Edited based on Aleph7's response.
更多推荐
弱自引用的泛型typeof
发布评论