标签:
@interface Kraken : NSObject @end @implementation Kraken + (instancetype)sharedInstance { static Kraken *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[Kraken alloc] init]; }); return sharedInstance; } @end
class TheOneAndOnlyKraken { class var sharedInstance: TheOneAndOnlyKraken { struct Static { static var onceToken: dispatch_once_t = 0 static var instance: TheOneAndOnlyKraken? = nil } dispatch_once(&Static.onceToken) { Static.instance = TheOneAndOnlyKraken() } return Static.instance! } }
class TheOneAndOnlyKraken { class var sharedInstance: TheOneAndOnlyKraken { struct Static { static let instance = TheOneAndOnlyKraken() } return Static.instance } }
private let sharedKraken = TheOneAndOnlyKraken() class TheOneAndOnlyKraken { class var sharedInstance: TheOneAndOnlyKraken { return sharedKraken } }
class TheOneAndOnlyKraken { static let sharedInstance = TheOneAndOnlyKraken() }
class TheOneAndOnlyKraken { static let sharedInstance = TheOneAndOnlyKraken() private init() {} //This prevents others from using the default ‘()‘ initializer for this class. }
FROM: http://krakendev.io/blog/the-right-way-to-write-a-singleton
THE RIGHT WAY TO WRITE A SINGLETON
标签:
原文地址:http://www.cnblogs.com/newBlash/p/4882650.html