Swift's single-case mode

Source: Internet
Author: User

Three ways to implement Singleton mode in Swift: global variables, internal variables, dispatch_once methods

1. Global variables

Private Let _singleton = Singleton ()  class Singleton:nsobject {      class var Sharedinstance:singleton {          get {
   return _singleton          }}}    

  

2. Internal variables

Class Singleton {      class var Sharedinstance:singleton {          get {              struct singletonstruct {                  static let sin Gleton:singleton = Singleton ()              }               return Singletonstruct.singleton          }      }  }  

  

3. Dispatch_once mode

Class Singleton {      class var Sharedinstance:singleton {          get {              struct singletonstruct {                  static var Oncetok en:dispatch_once_t = 0                  static var Singleton:singleton? = Nil              }              dispatch_once (& Singletonstruct.oncetoken, {(), Void in                  Singletonstruct.singleton = Singleton ()              })              return singletonstruct.singleton!          }}}  

  

Swift's single-case mode

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.