在Swift 3中调度一次

前端之家收集整理的这篇文章主要介绍了在Swift 3中调度一次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
swift 3中的dispatch_once的新语法是什么?旧版本如下。
var token: dispatch_once_t = 0
func test() {
    dispatch_once(&token) {
    }
}

这里是changes到libdispatch的Swift 3。

doc

Dispatch The free function dispatch_once is no longer available in
Swift. In Swift,you can use lazily initialized globals or static
properties and get the same thread-safety and called-once guarantees
as dispatch_once provided. Example:

let myGlobal = { … global contains initialization in a call to a closure … }() _ = myGlobal // using myGlobal will invoke the initialization code only the first time it is used.

猜你在找的Swift相关文章