IOS -- Singleton design mode in Swift development

Source: Internet
Author: User

IOS -- Singleton design mode in Swift development
Recently, I have developed a small application and encountered some common Singleton modes in Objective-c, but there are some differences in swift, I tried to find out that I couldn't play the cards by using common sense (normal oc to swift), so I searched some posts. It may be a problem with xcode or sdk (I believe they will not display untested code, right ?...), The code in some posts made obvious errors and compilation failed. So I will share this article with you. The original author implemented a Singleton, but the red code leads to non-thread security: 1 class var sharedInstance: TPScopeManager {2 get {3 struct Static {4 static var instance: TPScopeManager? = Nil 5} 6 7 if! Static. instance {8 Static. instance = TPScopeManager () 9} 10 11 return Static. instance! 12} 13} Now we have the following solutions (I personally feel very good): the first type of global constant: directly declare the global variable let _ SingletonSharedInstance = Singleton () class Singleton {...} advantage: the code is the most concise. Disadvantages: the code openness is messy. The second method makes up for the above shortcomings: private let _ SingletonSharedInstance = Singleton () class Singleton {class var sharedInstance: Singleton {return _ SingletonSharedInstance} note: because type constants (static constants of the class) are not supported, global constants are used here to support lasy initialization, because Swift will delay the initialization of global constants (and variables ), the let keyword is thread-safe. (Implication: global variables also delay initialization, but I'm not sure about thread security. Please kindly advise) Nested struct (estimated and translated as internal struct) class Singleton {class var sharedInstance: singleton {struct Static {static let instance: Singleton = Singleton ()} return Static. the instance} class does not support type constants (static constants of the class), but struct does. This can achieve similar results. We recommend that you use the internal struct method in the original article, unless the new version supports the type variable dispatch_once (this cannot be translated). The traditional OC method is also supported in Swift, compared with the previous method, this method obviously has no advantages, but write it out. 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 ()} return Static. instance!} (The same principle, or use struct support type variables this advantage, to the OC dispatch_once way to translate, refer to: http://www.codes51.com/article/detail_111374.html) as described above, apple has made it clear that delayed Initialization is thread-safe, so there is no need to add dispatch_once or similar protection measures. The essence of delayed loading of global variables (static members inside struct and enum are also the same) is dispatch_once. Therefore, if you want to use dispatch_once, you should declare a private global variable directly to ensure thread security, it will not make the code too open

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.