Created
September 7, 2016 07:46
-
-
Save aniltv06/6c18f26f29e811d8af236fb4f1f7eaf0 to your computer and use it in GitHub Desktop.
Singleton
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
import Foundation | |
class Singleton { | |
class var sharedInstance: Singleton { | |
struct Static { | |
static var onceToken: dispatch_once_t = 0 | |
static var instance: Singleton? = nil | |
} | |
dispatch_once(&Static.onceToken) { | |
Static.instance = Singleton() | |
print("Singleton") | |
} | |
return Static.instance! | |
} | |
} |
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
@implementation SingletonManager | |
+ (id)sharedManager { | |
static SomeManager *staticManager = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
staticManager = [[self alloc] init]; | |
print("Singleton"); | |
}); | |
return staticManager; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment