Singleton Class in Swift
Singleton is a creational design pattern that makes sure there is just one object of its kind and provides all other code with a single point of access to it. Singletons have essentially identical benefits and drawbacks as global variables. Despite being quite useful, they prevent your code from being modular. A class that depends on a Singleton cannot be used in another context without also importing the Singleton into that context. Or we can say that a Singleton design pattern assures that a class can contain only one object. The singleton implementations of FileManager, UserDefaults, UIApplication, and UIAccelerometer are frequently used on Apple’s platforms.
This is how a Swift singleton’s basic implementation looks:
Swift
// Swift program to illustrate singleton class Singleton { // This static let property that we are // constructing will be in charge of the // singleton instance. static let shared = Singleton() // Here, we are specifying the initializer // access level, which will restrict the // creation of additional instances. private init () { } // To accomplish a task, we are developing a method. func seekinfo() { // some task to execute print ( "w3wiki" ) } } // Here we are creating class's singleton instance. let singleton = Singleton.shared // Calling the method of singleton class. singleton.seekinfo() |
Output:
w3wiki
Example
Here is one example of using Class without Singleton pattern:
Swift
// Swift program to illustrate how to create a normal class class InformationManager { // Permission for Information func requestforInfo() { print ( "Grant info" ) } } // Access the class // Initialize the class let information = InformationManager() // Call your function here information.requestforInfo() |
Output:
Grant info
This class lacks the singleton pattern, so in order to use any method, we must initialize the class each time. To avoid this, singleton classes with static instances are used.
Rules to create a Singleton Class
Follow the following rules to create a singleton class:
A private initializer should be made
We can create a class object using an initializer. Additionally, restricting object creation from outside the class by making the initializer of a class. The InfoManager class’s initializer in this instance is private. Therefore, we have a problem when we attempt to construct an InfoManager object outside of the class.
Swift
// Swift program to illustrate singleton class InfoManager { ... // create a private initializer private init () { } } // Error let obj = InfoManager() |
Produce a Static Singleton Object Type
We generate a single static type object of the class in the singleton class. We can access the object by using the class name if we make it static.
Here, we are utilizing the class name InfoManager to access the InfoObj object.
Swift
// Swift program to illustrate singleton class InfoManager { // Static property to create singleton static let InfoObj = InfoManager() ... } // Accessing singleton let data = InfoManger.InfoObj |
Example 1
Swift
// Swift program to illustrate singleton class InfoManager{ // Singleton static let InfoObj = InfoManager() // Private initializer private init () { } // Request file func checkInfoAccess(user: String ) { // Check username if user == ( "@w3wiki.com" ) { print ( "Grant access" ) } else { print ( " Deny access" ) } } } let userName = "@w3wiki.com" // Accessing method let Info = InfoManager.InfoObj Info.checkInfoAccess(user: userName) |
Output:
Grant access
We have developed the singleton class InfoManager in the example above. We’ve generated a static object called InfoObj and made the initializer private because it’s a singleton class.
Example 2
Swift
// Swift program to illustrate singleton class InformationManager{ static let shared = InformationManager() var InformationGranted: Bool ? // Initializer access level change now private init (){} func requestForInfo() { // Code Process InformationGranted = true print ( "Grant info" ) } } // Access class function in a single line InformationManager.shared.requestForInfo() |
Output:
Grant info
Now you know how to create your Singleton class in your project, at last. Making it requires relatively little time. Simply to utilize within the project. It can be challenging to manage the lifecycle of your Singleton class if you use additional Singleton patterns in your applications. Additionally, it keeps a shared global mutable state. To avoid using the Singleton pattern excessively; dependency injection is preferable.
Contact Us