I'm creating a singleton class in Swift as follows:
class SingletonClass {
class var sharedInstance: SingletonClass {
struct Singleton {
static let instance = SingletonClass()
}
return Singleton.instance
}
var a: Int?
var b: Int?
var c: Int?
}
This allows me to access a shared instance from anywhere:
SingletonClass.sharedInstance
While this works, it doesn't make this instance the only possible one in the entire system, which is what singletons are all about.
That means that I can still create a whole new instance such as this:
let DifferentInstance: SingletonClass = SingletonClass()
And the shared instance is not the only one anymore.
So my question is this: Is there a way in Swift to create a true singleton class, where only one instance is possible system-wide?