Skip to content

Instantly share code, notes, and snippets.

@MrCloud
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrCloud/d04fb37b530b9c040840 to your computer and use it in GitHub Desktop.
Save MrCloud/d04fb37b530b9c040840 to your computer and use it in GitHub Desktop.
An Xcode Snippet for Singleton implementation in Swift
class <#ClassName#> {
class func sharedInstance() -> <#ClassName#> {
struct Static<#ClassName#> {
static let sharedInstance = <#ClassName#>()
}
return <#ClassName#>.sharedInstance
}
}
/* You might ask why there is no GCD related calls here with the obvious dispatch_once/token pair here.
Well in fact Swift does this for you under the hood, yes: Global and static properties are already dispatch_once'd
*/
@fantattitude
Copy link

Swift FTW !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment