Generic typeof for weak self references

keegan3d picture keegan3d · Jun 5, 2012 · Viewed 26.6k times · Source

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?

Answer

0xced picture 0xced · Jun 27, 2012

In the latest clang version Apple clang version 4.0 (tags/Apple/clang-421.1.48) (based on LLVM 3.1svn), i.e. Xcode 4.4+, the __typeof__((__typeof__(self))self) trick is not necessary anymore. The __weak typeof(self) bself = self; line will compile just fine.