I want to use the singleton pattern in my class which has a private init with parameter. It also has a class function called setup which configures
This seems to be the simplest way to implement the singleton in swift:
private let _AsteroidSharedInstance: Asteroid?
class Asteroid {
var config: ASTConfig?
class func setup(config: config) {
_AsteroidSharedInstance = Asteroid(config: config)
}
class var sharedInstance: Asteroid {
if _AsteroidSharedInstance == nil {
println("error: shared called before setup")
}
return _AsteroidSharedInstance
}
init(config: config) {
self.config = config
}
}
with the usage:
Asteroid.sharedInstance()
Source and Source