Swift: Strong Properties
TL;DR : StrongProperties.playground
I had some fun with creating the material to dig into strong references. In contrast to weak references, a strong reference increases and decreases the count used in automatic reference counting. Typically the concept of an owner is used to understand when and how to use strong and weak references. In the previous topic for Weak Vars I used outlets to show how to avoid a circular reference. Outlets and delegates are a very common scenario with iOS app development.
But there is not always a single owner for a variable. An object may hold into an instance of an object which is strong and this property can then be appended to an array which creates a new strong reference to that same instance. Setting the property to nil will not drop the reference count to zero because the array still has a strong reference to it. If the array were set to nil then all of the instances of that array would drop their reference count by one and possibly cause them to be deinitialized. In the example Playground made for this topic I set up that scenrio.
Friends in my Squad 👩👩👧
I created classes named Friend and Squad. I then created several friends and a squad and welcomed them all to the squad, but one member had to be suddenly kicked out of the squad, possibly due to bad blood. 🔪 It happens.
By opening up and running the playground named StrongProperties.playground you will see how the array holds onto the instances even after the variables are set to nil. Once the array is set to nil all of the instances are deinitialized. Toggle open the Debug Area at the bottom of Xcode to see the details which are printed out which show who is on the squad and when friends are ghosted.
Next: Property Observers
GitHub Gist