Rocking the Singleton Pattern in Swift

UPDATE: KrakenDev published an updated Singleton pattern in Swift. Swift 1.2 gave us static class members which means we can amend our Singleton pattern be:

class Singleton {
    static let sharedInstance = Singleton();
    private init() {}
}

 

The Singleton Pattern is a common design pattern in software engineering.

The Singleton Pattern ensures, a class has only one instance, and provides a global point of access to it

The Singleton Pattern is useful when exactly object is needed. For example you may choose to implement a Logger as a Singleton to provide global access to a log.

A Singleton has two intents.

  1. Ensure a class has only one instance
  2. Provide a global access point

In Objective-C it was common to use a static variable to hold the instance of the Singleton class. The use of dispatch_once took care of any multi-threading issues.

  1. Uses a static variable to hold an instance of the Singleton class.
  2. If the SharedInstance variable has not already been defined instantiate a new Singleton object.
  3. We don’t want people instantiating our class using the default initializer so amend the initializer to raise an exception.
  4. A secret initializer is used to call [super init] initializing a new instance of the Singleton class.

The two intents of a Singleton, ensure a class has only one instance. and provides a global access point. Through making the default initializer raise an exception we cover off one of the intents. By exposing a method sharedInstance to return the one instance of a Singleton object we cover off the other intent, provide a global access point.

So how do you go about implementing the Singleton Pattern in Swift?

  1. Define a class property  sharedInstance which returns an instance of SingletonClass.
  2. We need to hold the one instance of SingletonClass, static variables are a common way of achieving this. By wrapping the static variable instance in a struct it ensures thread safety.

    The lazy initializer for a global variable (also for static members of structs and enums) is run the first time that global is accessed, and is launched as dispatch_once to make sure that the initialization is atomic. This enables a cool way to use dispatch_once in your code: just declare a global variable with an initializer and mark it private.

  3. We need to ensure a consumer of the SingletonClass can’t call SingletonClass() to get a new instance. We must ensure they use SingletonClass.sharedInstance. This caused me a lot of headache finding a way to accomplish this Stackoverflow Article helped lots though.

Areas for improvement

I think wrapping the static variable instance in a struct is simple and elegant

As the app grows and you have more Singleton classes there is a lot of repetition in the init() function. It would be beneficial to encapsulate the assertion logic.

1 reply

Trackbacks & Pingbacks

  1. […] We are going to manage user authentication from a Singleton class LoginService. I have described the Singleton Pattern in Swift in the following blog post Rocking the Singleton Pattern in Swift. […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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