Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // final prevents class to be subclassed.
- final class Singleton {
- // A variable which stores the singleton object.
- // On initialization This is how we create a singleton object.
- static let sharedInstance = Singleton()
- // Private initialization to ensure just one instance is created.
- private init() {
- print("Initialized.")
- }
- func sayHi() {
- print("Hi!")
- }
- }
- let instance = Singleton.sharedInstance
- instance.sayHi()
- // Next line will fail
- Singleton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement