Singleton Class in Swift

Getting started with Singleton Class. | What is the Singleton pattern?

Anand Nimje
2 min readApr 1, 2018

Singleton is a design pattern that is very popular in development. Most of the developers are using this design pattern. This is very simple, common and easy to use in your project. It initializes your class instance single time only with static property and it will share your class instance globally.

We have used so many times Apple’s Foundation APIs like - UserDefaults.standard , FileManager.default . Which are similar to singleton class pattern.

Here the one simple example of using class

class LocationManager{//MARK: - Location Permission
func requestForLocation(){
//Code Process
print("Location granted")
}

}
//Access the class
let location = LocationManager() //initialization class
location.requestForLocation() //Call function here

This is the class without a singleton pattern for access to any function we need to initialize class every time for avoiding these things we are using singleton classes with the static instance.

Write your first Singleton Class 👍🏻

class LocationManager{

static let shared = LocationManager()

init(){}

func requestForLocation(){
//Code Process
print("Location granted")
}

}
//Access class function with Singleton Pattern 🚀
LocationManager.shared.requestForLocation() //"Location granted"
//Still you can use your class like this
let location = LocationManager()
location.requestForLocation()

A better way to write your Singleton class 😎

class LocationManager{

static let shared = LocationManager()

var locationGranted: Bool?
//Initializer access level change now
private init(){}

func requestForLocation(){
//Code Process
locationGranted = true
print("Location granted")
}

}
//Access class function in a single line
LocationManager.shared.requestForLocation()

After changing the access level of initializer you will get this kind of errors -

Changes in Access level of initializer

Every class having default public initializer, its change to now private. Now you can’t initialize your singleton class again.

How to use Singleton 🎉

//In a single line you can access easily 
LocationManager
.shared.requestForLocation() // "Location granted"
//Access variable value
print(
LocationManager.shared.locationGranted ?? false) // true

Conclusion

Finally, you got understanding now how to create your Singleton class in your project. It’s taking very little time to create. Easy to use inside the project. It’s having some advantages and disadvantages. If you using more Singleton patterns inside your projects it’s hard to manage the lifecycle of your Singleton class. Also, it maintains a global mutable shared state. Try to avoid overuse of Singleton pattern better to use dependency injection. 👍🏻

Dependency injection i will cover in next topic soon. 😊

Thanks for reading 🙌🏼

If you having any queries regarding this tutorial? | If you think you can do more simple way or little bit more extra things with this stuff please let me know questions, feedback or comments -on Twitter

--

--