19

I am learning about Singleton pattern in swift and efficient way to create a Singleton class and found out the best way to create as below.

class SingletonClass{
    static let sharedInstance = SingletonClass()
}

Since i have use the let statement it is read only property and has to be thread safe so there is no need of dispatch_once() as of Objective C.And static is used to make the sharedInstance variable as a class variable i guess.

But how does this guarantee there is only one instance created throughout the application?is there a small thing i am missing?

  • 2
    How many copies of this class (as opposed to *objects* of this class) do you think there will be? – Scott Hunter Jun 24 '15 at 17:16
  • one copy of this class...but i may access this as SingletonClass.sharedInstance at many places... –  Jun 24 '15 at 17:17
  • 2
    Are you suggesting that accessing it somehow makes more copies? – Scott Hunter Jun 24 '15 at 17:18
  • 2
    more than one instance of singletonclass can be created, however sharedInstance will only be created once. – Will M. Jun 24 '15 at 17:20
  • @ScottHunter not suggesting..why it doesnot make many copies because i am creating new instance as static let sharedInstance = SingletonClass()....so what stopped me as there will be the same instance everywhere? –  Jun 24 '15 at 17:20
  • @WillM. how sharedInstance is created only once..in my case...? –  Jun 24 '15 at 17:21
  • 2
    @copeME it is a static variable that is immutable. Once it is created, it cannot be reassigned, as it was created with let. Since it is a static variable, it is associated with the class, not instances of the class. – Will M. Jun 24 '15 at 17:26
  • Duplicated with question: http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift – David Liu Jun 24 '15 at 17:40
  • nope ..its not..i am trying to know how it works too –  Jun 25 '15 at 02:39
  • [How to create Singleton in Swift 2.0](http://stackoverflow.com/questions/26742138/singleton-in-swift/33420040#33420040) – Himanshu Mahajan Oct 29 '15 at 16:45
  • [How to create Singleton in swift 2.0](http://stackoverflow.com/questions/26742138/singleton-in-swift/33420040#33420040) – Himanshu Mahajan Oct 29 '15 at 16:47

4 Answers4

25

If you want to prevent instantiation of your class (effectively restricting usage to only the singleton), then you mark your initializer as private:

class SingletonClass {

    static let shared = SingletonClass()

    private init() {
        // initializer code here
    }
}
Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
10

What guarantees it is only created once is the keyword static. you can reference this article: https://thatthinginswift.com/singletons/

Hope that helps.

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs. In simple terms, it means that you can call a method, even if you've never created the object to which it belongs

wilforeal
  • 369
  • 3
  • 13
  • 1
    you just dont declare static so that you dont need to create an object to access the variable? be more specific why to declare static in this case? –  Jun 25 '15 at 04:15
5

Make private init, for example :

final class Singleton {

    // Can't init is singleton
    private init() { }

    //MARK: Shared Instance

    static let sharedInstance: Singleton = Singleton()

    //MARK: Local Variable

    var emptyStringArray : [String] = []

}
YanSte
  • 10,661
  • 3
  • 57
  • 53
1

You're right. And you may want to read Files and Initialization about how global and static variable are handle in Swift

Swift use this approach

Initialize lazily, run the initializer for a global the first time it is referenced, similar to Java.

It says

it allows custom initializers, startup time in Swift scales cleanly with no global initializers to slow it down, and the order of execution is completely predictable.

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.

onmyway133
  • 45,645
  • 31
  • 257
  • 263