Singleton_Pattern
Singleton Builder Pattern
class MySingleton {
static let shared = MySingleton()
struct Config {
let param:String
}
private static var config:Config?
class func setup(_ config:Config){
MySingleton.config = config
}
private init() {
guard let config = MySingleton.config else {
fatalError("Error - you must call setup before accessing MySingleton.shared")
}
//Regular initialisation using config
}
}
Init
MySingleton.setup(MySingleton.Config(param: "Some Param"))
Access Singleton
MySingleton.shared