Skip to content

Instantly share code, notes, and snippets.

@qRoC
Created July 8, 2020 17:38
Show Gist options
  • Save qRoC/533da988ecf85bab95ed20bb3183e63e to your computer and use it in GitHub Desktop.
Save qRoC/533da988ecf85bab95ed20bb3183e63e to your computer and use it in GitHub Desktop.
Swift Singleton
#if canImport(Darwin)
import Darwin
#else
import Glibc
#endif
class Singleton {
static var shared: Singleton! = Singleton()
private init() {
print("init")
atexit {
Singleton.shared = nil
}
}
deinit {
print("deinit")
}
func test() {
print("test")
}
}
Singleton.shared.test()
init
test
deinit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment