Skip to content

Instantly share code, notes, and snippets.

@romainmenke
Last active December 2, 2015 16:41
Show Gist options
  • Save romainmenke/5bbfafecf7820d54426e to your computer and use it in GitHub Desktop.
Save romainmenke/5bbfafecf7820d54426e to your computer and use it in GitHub Desktop.
protocol SingletonDelegate : class {
func reportSingletonUpdate(value:Int)
}
class Singleton {
private static var privateShared : Singleton?
weak var delegate : SingletonDelegate?
class func shared() -> Singleton {
guard let uwShared = privateShared else {
privateShared = Singleton()
return privateShared!
}
return uwShared
}
class func destroy() {
privateShared = nil
}
func somethingHappened() {
guard let del = delegate else {
return
}
del.reportSingletonUpdate(0)
}
private init() {
}
}
class VC : UIViewController, SingletonDelegate {
override func viewDidAppear(animated: Bool) {
Singleton.shared().delegate = self
}
func reportSingletonUpdate(value: Int) {
print(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment