Swift singletons are exposed in the Cocoa frameworks as class functions, e.g. NSFileManager.defaultManager()
, NSNotificationCenter.defaultCenter()
, so I feel. So it makes more sense as a class function to mirror this behaviourbehavior, rather than a class variable as some other solutions use,. e.g.:
class MyClass {
private static let _sharedInstance = MyClass()
class func sharedInstance() -> MyClass {
return _sharedInstance
}
}
Retrieve the singleton via MyClass.sharedInstance()
.