Singletons in Swift

Singletons in Swift

First of all, for those who are not familiar a singleton is an object which is instantiated once. Only one copy of this object exists in memory and the state is reachable by any other object.
I really like simplicity in code especially for code that is most frequently used, and that’s what Swift is mainly focused on. In this post I will show you how to initialize a singleton class:

 

In your singleton class: 

    static let sharedInstance = YourClass()

 

Usage in other classes:

var yourClass = YourClass.sharedInstance

 

That would be it for this tutorial, a really clean implementation of singleton classes.

 

Leave a Reply

Your email address will not be published. Required fields are marked *