Swift Retain Cycles and Strategies to Prevent Them

There are many articles written on this subject. Many of them suggest using Xcode’s memory graph debugger. Is there an easier way? I say yes! Unit tests to the rescue! The idea of the unit test is: Instantiate the object under test (“target”) and assign it to a nullable variable. Create a weak variable (“weakTarget”) and assign the target to it On the target, call a function to test for retain cycles Set target to nil Test weakTarget for nil, if it is not nil, there is a retain cycle For example, let’s say you have: To unit test for … Continue reading Swift Retain Cycles and Strategies to Prevent Them