Singleton in Swift

I first learned how to implement the Singleton pattern in Swift in this book, Pro Design Patterns in Swift .

The way I started implementing the Singleton template is in the following example:

 class Singleton { class var sharedInstance: Singleton { struct Wrapper { static let singleton = Singleton() } return Wrapper.singleton } private init() { } } 

But then I found this implementation while reading Cocoa Design Patterns

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

So my question is, what is the difference between the two implementations?

+5
source share
1 answer

Back in Swift 1 day, static let has not yet been implemented. The workaround was to create a struct wrapper. With Swift 2, this is no longer necessary.

+4
source

Source: https://habr.com/ru/post/1243467/


All Articles