December 2018
Intermediate to advanced
414 pages
10h 19m
English
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 = Earth() private init() {} func spinAroundTheSun() { /* */ }}let planetEarth = Earth.instanceplanetEarth.spinAroundTheSun()
Here, we have an Earth class. There ...
Read now
Unlock full access