Swift Language Implementation Singleton mode

Source: Internet
Author: User


Swift implements Singleton mode

The singleton pattern is implemented in every language, and the swift language has been available for a few days. Look at the document through these days. We have written a single example of Swift, for everyone to learn to communicate, please correct me.


---if reproduced please specify the source, I GitHub blog new address-Yueruo ' s Blog-http://yueruo.github.io---


Because Swift language weakens the boundary between struct and class, here I give my own two kinds of single-instance implementations

Class version number:
class SwiftSingleton{    class func shareInstance()->SwiftSingleton{        struct YRSingleton{            static var predicate:dispatch_once_t = 0            static var instance:SwiftSingleton?

= nil } dispatch_once(&YRSingleton.predicate,{ YRSingleton.instance=SwiftSingleton() } ) return YRSingleton.instance! }}

For singleton classes. Requires a unique shareinstance method for the external output instance. And through the official documentation of the search, found that for class. Static methods can be used class func to mark. Static variables are used for class var processing, but here I use the static variables of the internal struct to store the unique instance.

Called, you can use the

var swiftInstance1=SwiftSingleton.shareInstance()var swiftInstance2=SwiftSingleton.shareInstance()if swiftInstance1===swiftInstance2{//“===”判别是否是同一个实例对象    println("they are the same instance!")}

In addition, the above uses the Dispatch_once, has had the GCD programming experience to be familiar, can guarantee the thread security, and only then will be called once.



struct version number

The struct version number is almost identical to the class version number, the only difference being that the keyword used by func is class func changed tostatic func

struct StructSingleton{    static func shareInstance()->StructSingleton{        struct YRSingleton{            static var predicate:dispatch_once_t = 0            static var instance:StructSingleton?

= nil } dispatch_once(&YRSingleton.predicate,{ YRSingleton.instance=StructSingleton() } ) return YRSingleton.instance! }}

Swift Language Implementation Singleton 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.