Singletons in a nutshell

In Swift, you may want to use a singleton in order to ensure only a single instance of your object is ever created. This is very often seen in the facade pattern.

Let's see how to go about creating a singleton:

  1. Identify the class you want to refactor as a singleton, ensuring it doesn't grow in memory too much over time
  2. Make the initializer private. A singleton can never be instantiated from outside
  3. Add a static immutable member, which will be the single instance of your object
  4. Replace all occurrences of the now inaccessible constructor with the shared reference