美文网首页
swift单例

swift单例

作者: anyurchao | 来源:发表于2016-11-28 17:13 被阅读17次

普通版单例

class TheOneAndOnlyKraken {

static let sharedInstance = TheOneAndOnlyKraken()

//This prevents others from using the default '()' initializer for this class.

private init() {}   

}       

多线程版单例

class func theOneAndOnlyKraken() -> TheOneAndOnlyKraken{
    struct Singleton {
        static var predicate:dispatch_once_t = 0
        static var instance:TheOneAndOnlyKraken? = nil
    }
    dispatch_once(&Singleton.predicate, {
        Singleton.instance = TheOneAndOnlyKraken()
    })
    return Singleton.instance!
}

相关文章

网友评论

      本文标题:swift单例

      本文链接:https://www.haomeiwen.com/subject/ybvopttx.html