How to avoid “Capturing ‘self’ strongly in this block is likely to lead to a retain cycle”

I was trying to update a UILabel from inside a block the other day, and was puzzled by the above Xcode warning: “Capturing ‘self’ strongly in this block is likely to lead to a retain cycle”. The full code looked like this:


As it turns out, the trouble was that a block executed in self cannot capture a reference to itself (self) without there being a never ending loop involved. It’s probably to complicated for me to understand and/or care about, but thankfully there’s a relatively easy way to correct this problem.

All we need to do is to create a weak reference to self and use it in our block instead. Method calls and property access will still work, but the strong retain cycle no longer exists, making the compiler happy. And that is, after all, the most important thing in the world: to make some frigging software happy.

Here’s the amended version, using a typeof weakSelf. It can be used just like self inside the block:

Thanks to Tim on StackOverflow. Read his full answer and in-depth discussion here:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.