Skip to content

Instantly share code, notes, and snippets.

@HoldFast88
Last active August 29, 2015 14:02
Show Gist options
  • Save HoldFast88/6258fb1693bc19de7a85 to your computer and use it in GitHub Desktop.
Save HoldFast88/6258fb1693bc19de7a85 to your computer and use it in GitHub Desktop.
Threat-safe Swift singleton example
class <#ClassName#> : NSObject {
class var sharedInstance: <#ClassName#> {
get {
struct Static {
static var instance : <#ClassName#>? = nil
static var token : dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = <#ClassName#>()
}
return Static.instance!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment