The commonly accepted Singleton pattern for Swift uses a Struct inside a class variable/type property.
Instead of:
class MySingleton {
class var sharedInstance: MySingleton {
struct Singleton {
static let instance = MySingleton()
}
return Singleton.instance
}
}
Why do we not just do:
class MySingleton {
class var sharedInstance: MySingleton {
let instance = MySingleton()
return instance
}
}
Apologies if this is a very stupid question. But don't both leverage the thread-safety of constants and let
?