Last active
February 19, 2022 02:01
-
-
Save wh1pch81n/42e5c62762a9c8eaf470cbf787d7b120 to your computer and use it in GitHub Desktop.
The Swift way of doing Singleton inheritance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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