Why Your Lazy Vars Aren’t Creating Strong Reference Cycles in iOS

Even when they capture self

Photo by freestocks on Unsplash

The Background

1. Lazy Properties

2. Closures and Reference Cycles

Getting Closer…

// Lazy var capturing unowned self
lazy var myLazyVariable = { [unowned self]
return self.someValue * 5
Playground with lazy var closure capturing unowned ref to self
Playground output: No reference cycle created

…The Big Finish

Lazy prop closure with no capture list and implicit use of self
Output: No compiler error and no strong reference cycle created
Original tweet here

Software developer. Mobile, serverless, voice. Knows some things, curious about all things.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store