Singleton Class in Swift
Last Updated :
10 Mar, 2023
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
class Singleton
{
static let shared = Singleton()
private init () { }
func seekinfo()
{
print ( "GeeksforGeeks" )
}
}
let singleton = Singleton.shared
singleton.seekinfo()
|
Output:
GeeksforGeeks
Example
Here is one example of using Class without Singleton pattern:
Swift
class InformationManager
{
func requestforInfo()
{
print ( "Grant info" )
}
}
let information = InformationManager()
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
class InfoManager {
...
private init () {
}
}
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
class InfoManager {
static let InfoObj = InfoManager()
...
}
let data = InfoManger.InfoObj
|
Example 1
Swift
class InfoManager{
static let InfoObj = InfoManager()
private init ()
{
}
func checkInfoAccess(user: String )
{
if user == ( "@geeksforgeeks.com" )
{
print ( "Grant access" )
}
else {
print ( " Deny access" )
}
}
}
let userName = "@geeksforgeeks.com"
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
class InformationManager{
static let shared = InformationManager()
var InformationGranted: Bool ?
private init (){}
func requestForInfo()
{
InformationGranted = true
print ( "Grant info" )
}
}
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.
Share your thoughts in the comments
Please Login to comment...