美文网首页
swift单例

swift单例

作者: VervertomJC | 来源:发表于2022-12-08 12:56 被阅读0次

原理:利用静态成员自动满足dispatch_once规则

class Singleton {
    static let shared = Singleton()
    /*
     static let shared = {
     let instance = Singleton()
     return instance
     }()
     */
    private init(){}
}

利用全局变量自动满足dispatch_once规则

private let instance = Singleton()
final class Singleton {
    static var shared: Singleton {
        instance
    }
    fileprivate init() {}
}

参考:总结Swift5单例的几种写法和常见错误写法

相关文章

网友评论

      本文标题:swift单例

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