Skip to content

Instantly share code, notes, and snippets.

@yannxou
Last active October 19, 2015 07:26
Show Gist options
  • Save yannxou/40bb29b28f89ee1a6e71 to your computer and use it in GitHub Desktop.
Save yannxou/40bb29b28f89ee1a6e71 to your computer and use it in GitHub Desktop.
Swift: Singleton object example
/// 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