Group Group Group Group Group Group Group Group Group

When to use [weak self]

Hello,
I’m currently in chapter 8 (starting to feel a bit overwhelmed with so many diff types of operators and all), not sure if I can make the judgement on when to use which.
Anyway, my main question is, when do I actually use [weak self]? I noticed that in some mapping it is used some isn’t.

If you are capturing a class instance in a closure you should use [weak self] to tell the compiler that you do not want to explicitly retain that object.

Sometimes, if the app is just one view controller (like some of the demo apps in the book) we might skip [weak self] because it won’t make any difference - that single view controller will anyways be retained in memory for as long as the app runs.

More about memory management in Swift here: https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html

This topic was automatically closed after 166 days. New replies are no longer allowed.