swift中的dispatch_once_t替代方案

iPh*_*sor 0 swift

已经试过这些:

'dispatch_once_t'在Swift中不可用:改为使用延迟初始化的全局变量

Swift 3中的dispatch_once是哪一个?

这是我的代码: 在此输入图像描述

class var sharedInstance:Model{
        struct Static{
            static var instance:Model?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token){
            Static.instance = Model()
        }
        return Static.instance!

    }
Run Code Online (Sandbox Code Playgroud)

请建议我dispatch_once_t的替代方法..我不知道swift,我代码C/C++/Obj.C,请给我快速代码修复上面的问题

ade*_*dev 5

在swift singleton中可以写成,

class Model: NSObject {
    static let sharedInstance = Model()
}
Run Code Online (Sandbox Code Playgroud)

然后用Model.sharedInstance.你不需要像目标c那样派遣一次.

来源https://thatthinginswift.com/singletons/