
Hands-On Design Patterns with Swift
By :

The singleton pattern is one of the most used (and misused) creational patterns. It is seen throughout the Cocoa libraries, UIKit, AppKit, and many other Apple frameworks. Be aware that its use can lead to a central state point that gets mutated across your whole program, which can lead to nasty bugs.
In this section, we'll go back to basics with the singleton and focus on its use cases.
Singletons are fairly easy in Swift. A singleton is an object of which there can never be more than one instance in your program. The first instance of a singleton object will be the last. As a corollary, singletons never die, and their lifespan is always your whole program.
In Swift, we have to be particularly cautious about retain cycles. Any object that will be retained by your singleton will ultimately live till the end of your program. It's not an understatement to say: you have to be very cautious with them.
Here's an example:
class Earth { static let current...