Last active
August 29, 2015 14:02
-
-
Save soulfly/ba799017ec7e1922b730 to your computer and use it in GitHub Desktop.
Swift singleton implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myInstance:ChatService? | |
class ChatService { | |
class func instance() -> ChatService{ | |
if !myInstance { | |
myInstance = ChatService() | |
println("init") | |
} | |
return myInstance! | |
} | |
} | |
... | |
var a = ChatService.instance() | |
var b = ChatService.instance() | |
println(a===b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this threat-safe solution https://gist.github.com/HoldFast88/6258fb1693bc19de7a85