Questions tagged «dispatch»

29
在Swift中使用dispatch_once单例模型
我正在尝试制定一个合适的单例模型以在Swift中使用。到目前为止,我已经能够获得一个非线程安全模型,其工作方式如下: class var sharedInstance: TPScopeManager { get { struct Static { static var instance: TPScopeManager? = nil } if !Static.instance { Static.instance = TPScopeManager() } return Static.instance! } } 在静态结构中包装单例实例应该允许一个不与单例实例冲突的单实例,而无需复杂的命名方案,这应该使事情变得相当私有。但是,显然,此模型不是线程安全的。所以我尝试添加dispatch_once到整个事情: class var sharedInstance: TPScopeManager { get { struct Static { static var instance: TPScopeManager? = nil static var token: dispatch_once_t …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.