Last active
October 19, 2015 07:26
-
-
Save yannxou/40bb29b28f89ee1a6e71 to your computer and use it in GitHub Desktop.
Swift: Singleton object example
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
/// Swift 2 syntax | |
class MyManager: NSObject { | |
static let sharedInstance = MyManager() | |
// Ensure the class cannot be initialized from outside by declaring a private initializer | |
private override init() { | |
super.init() | |
// Do your internal initialization privately | |
} | |
} | |
/// Swift 1.x syntax | |
class MyManager: NSObject { | |
class var sharedInstance : MyManager { | |
struct Static { | |
static let instance : MyManager = MyManager() | |
} | |
return Static.instance | |
} | |
// Ensure the class cannot be initialized from outside by declaring a private initializer | |
private override init() { | |
super.init() | |
// Do your internal initialization privately | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment