Swift单例的几种方式

单例的三种方式

1.Class constant
static let sharedInstance = Weatcher()
2.dispatch_once

   class var sharedInstance : Weatcher {
        struct Static {
            static var onceToken : dispatch_once_t = 0
            static var instance : Weatcher? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = Weatcher()
        }
        return Static.instance!
    }

3.Nested struct

    class var sharedInstance: Weatcher {
        struct Static {
            static let instance: Weatcher = Weatcher()
        }
        return Static.instance
    }```

你可能感兴趣的:(Swift单例的几种方式)