There is no advantages of using this in Swift, but in case you have Objective-C you can do it using:
1 2 3 4 5 6 7 8 9 10 11 12 |
class Singleton { class var sharedInstance : Singleton { struct Static { static var onceToken : dispatch_once_t = 0 static var instance : Singleton? = nil } dispatch_once(&Static.onceToken) { Static.instance = Singleton() } return Static.instance! } } |