??????????????????????????ж???????????????????????????????????????????????????????Swift????????????????????????鷳?£???????????????????????????????????μ?????????????????Swift??????????????????
?????????????????Swift?е????????????????????????????ɡ?
????????????ObjC????
????Swift??Objective-C??????????????????μ???????????
????@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
????????????????У????????????????????????????????????Щ???????????????????
????????????
???????????????????????????????μ??
????1?????????????(???????е?????) ?????????????????????????????????????????????????????????????????磺
????NSNotificationCenter?? UIApplication??NSUserDefaults??
????2??????????????????????????????????????е??????????????????????????????????????????
????3???????????1??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????dispatch_once????????????????????????Ρ?
??????????б???????????????????Σ????????????????????2????У??????????????????????????dispatch_once????
????Swift????
??????Swift 1.0??????????????к???????????Щ??????????к???????????????
????· SwiftSingleton
????· dispatch_once singleton model in swift
????· Apple Swift Blog
???????????????????????????°???????4???汾?????????????£?
????1. ??a??????Swift???Objective-C???
????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!
????}
????}
????????汾??Objective-C?????????檔?????????????????Swift???????????????????????????????????????????á?
????2. ???巽????“????????
????class TheOneAndOnlyKraken {
????class var sharedInstance: TheOneAndOnlyKraken {
????struct Static {
????static let instance = TheOneAndOnlyKraken()
????}
????return Static.instance
????}
????}
????Swift 1.0???????????????????????????????????????????y??壬???????????????????????????????????????????????????????С????Objective-C????汾???Щ???????????á???????????Swift 1.2?????????o???????????????д??????????????????????
????3. ????????????“???е???”??????
????private let sharedKraken = TheOneAndOnlyKraken()
????class TheOneAndOnlyKraken {
????class var sharedInstance: TheOneAndOnlyKraken {
????return sharedKraken
????}
????}
??????Swift 1.2?????????????????????(access control specifiers) ????????????(static class members)??????ζ?????????????????????????????????????????????????????????????汾????????Swiftier???
????????????????????????ο?????dispatch_once??????Apple Swift?????е??????????????????????dispatch_once?????????и???????????dispatch_once?????????????á?
????“???????????н???????????????????Lazy????????????????????????????Ρ??????????dispatch_once????????????????????????????????'???ε???'???????????????????????е????????????ɡ?”--????Apple's Swift Blog
??????“The lazy initializer for a global variable (also for static members of structs and enums) is run the first time that global is accessed?? and is launched as dispatch_once to make sure that the initialization is atomic. This enables a cool way to use dispatch_once in your code: just declare a global variable with an initializer and mark it private.”??
????????Apple??????????????????????????Щ??????????????????壯????????????????dispatch_once??????????????????????????????“?????”????????????????dispatch_once????????????????????????????????????????Σ?
????????????????????????????????????
???????????????????“???е?????”???????????????????
????class TheOneAndOnlyKraken {
????static let sharedInstance = TheOneAndOnlyKraken()
????}
???????????????????????????о?????????????????????????????Capital One????ζ?????????review?????????У?????????????App?????Swift??д???????μ???????????????????д??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????鯔?????????????????????????????
??
??????????????????????????????google?????????10?????????????????????????????????е???????????????????????????????????????????