You don’t (always) need [weak self]

Besher Al Maleh
Jun 17 · 13 min read
Thanks to Arpit Sharma for the inspiration!

Automatic Reference Counting

let changeColorToRed = DispatchWorkItem { [weak self] in
self?.view.backgroundColor = .red
}

Unowned, Weak, and the Strong-Weak Dance

note that ‘WeakSelf’ was the name of the app that crashed

Escaping vs non-escaping closures

Delayed Deallocation

‘guard let self = self’ vs Optional Chaining

guard let syntax can lead to delayed deallocation
Optional chaining leads to no delayed deallocation

Examples

Grand Central Dispatch

UIView.Animate and UIViewPropertyAnimator

Storing a function call in a property

Timers

Demo App

The staircase is not a good sign!

Alternatives to [weak self]

Conclusion

Thanks for reading. If you enjoyed this article, feel free to hit that clap button 👏 to help others find it. If you *really* enjoyed it, you can clap up to 50 times 😃

Check out some of my other articles:

References used for this article:

Demo App:

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Besher Al Maleh

Written by

Self-taught iOS developer interested in all things iOS www.besher.ca

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing