Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Last active February 19, 2022 02:01
Show Gist options
  • Save wh1pch81n/42e5c62762a9c8eaf470cbf787d7b120 to your computer and use it in GitHub Desktop.
Save wh1pch81n/42e5c62762a9c8eaf470cbf787d7b120 to your computer and use it in GitHub Desktop.
The Swift way of doing Singleton inheritance.
import UIKit
class Singleton {
class func sharedInstance() -> Singleton {
struct __ { static let _sharedInstance = Singleton() }
return __._sharedInstance
}
var name: String = String()
}
class SingletonSubclass: Singleton {
override class func sharedInstance() -> SingletonSubclass {
struct __ { static let _sharedInstance = SingletonSubclass() }
return __._sharedInstance
}
}
Singleton.sharedInstance().name // ""
SingletonSubclass.sharedInstance().name // ""
SingletonSubclass.sharedInstance().name = "9"
Singleton.sharedInstance().name // ""
SingletonSubclass.sharedInstance().name // "9"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment