A trip down MEMORY LANE
@interface Kraken:nsobject @end @implementation Kraken+ (instancetype) sharedinstance { static Kraken *sharedinstance = nil; Static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{ = [[Kraken alloc] init]; }); return sharedinstance;} @end
the ugliest (a.k.a. The "Why is still CODING in SWIFT IF you ' RE JUST going to do this" "
class Theoneandonlykraken { class var sharedinstance:theoneandonlykraken { struct Static { static0 static var instance:theoneandonlykraken? = Nil } Dispatch_once (&static.oncetoken) { = theoneandonlykraken () } return static.instance! }}
The STRUCT (a.k.a. The "old but strangely still POPULAR")
class Theoneandonlykraken { class var sharedinstance:theoneandonlykraken { struct Static { static Let instance = Theoneandonlykraken () } return static.instance }}
The GLOBAL VARIABLE (a.k.a. The "one line SINGLETON")
private Let Sharedkraken = theoneandonlykraken ()class theoneandonlykraken { class var sharedinstance:theoneandonlykraken { return sharedkraken }}
The right-a.k.a. "The One line SINGLETON (now with proof!")
class Theoneandonlykraken { static Let sharedinstance = Theoneandonlykraken ()}
DON ' T Forget the PRIVATE init!
class Theoneandonlykraken { static Let sharedinstance = theoneandonlykraken () Private//Thisprevents others from using the default ' () ' initializer for this class.}
From:http://krakendev.io/blog/the-right-way-to-write-a-singleton
The right-to-WRITE A SINGLETON