Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. // final prevents class to be subclassed.
  2. final class Singleton {
  3. // A variable which stores the singleton object.
  4. // On initialization This is how we create a singleton object.
  5. static let sharedInstance = Singleton()
  6.  
  7. // Private initialization to ensure just one instance is created.
  8. private init() {
  9. print("Initialized.")
  10. }
  11.  
  12. func sayHi() {
  13. print("Hi!")
  14. }
  15. }
  16.  
  17. let instance = Singleton.sharedInstance
  18. instance.sayHi()
  19.  
  20. // Next line will fail
  21. Singleton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement