Skip to content

Instantly share code, notes, and snippets.

@ankitthakur
Created November 13, 2018 19:26
Show Gist options
  • Save ankitthakur/e6650172a6b0ce1f1fb67d462fdcff1b to your computer and use it in GitHub Desktop.
Save ankitthakur/e6650172a6b0ce1f1fb67d462fdcff1b to your computer and use it in GitHub Desktop.
Singleton Swift class
final class MyClass {
private init() {}
private static var _shared: MyClass?
public static var shared: MyClass {
get {
if _shared == nil {
DispatchQueue.global().sync(flags: .barrier) {
if _shared == nil {
_shared = MyClass()
}
}
}
return _shared!
}
}
func someFuntion() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment