Javatpoint Logo
Javatpoint Logo

Singleton class in swift

Singleton is a design pattern that is widely used in iOS development. Most of the developers use this design pattern to develop applications. Singleton's design pattern allows us to share our class instance globally within our application. Singleton class is the one that can be instantiated using only once during the lifetime of the application.

If we notice, we have already used many of the singleton classes provided as Apple's Foundation APIs like UserDefaults.standard, FileManager.default, etc.

Let's consider the following example, which contains a simple class without using the Singleton design pattern.

In the above example, the class LocationPermission is not a Singleton class. To use the getPersmission() method of LocationPermission class, we need to instantiate the class every time. Let's consider the following example to look into the process of creating the Singleton class.

The above class has become Singleton if we make its constructor private. However, the singleton class must have a static variable that returns the object of the class.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA