The first time I learned how to Implement Singleton Pattern in Swift is in this Book Pro Design Patterns in Swift
.
The way I started implementing the Singleton Pattern is in the example below:
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 about Cocoa Design Patterns
class Singleton {
static let sharedInstance = Singleton()
private init() {
}
}
So my question is, what's the difference between the two implementations ?
Back in Swift 1 days, static let
wasn't implemented yet. The workaround was to create a wrapper struct
. With Swift 2, this is not needed anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With