当前位置 : 主页 > 手机开发 > 其它 >

Swift 3 GCD API更改后的dispatch_once

来源:互联网 收集:自由互联 发布时间:2021-06-11
在语言版本3中进行更改后, Swift中dispatch_once的新语法是什么?旧版本如下. var token: dispatch_once_t = 0func test() { dispatch_once(token) { }} 制作了这些are the changes to libdispatch. 从 doc: Dispatch The f
在语言版本3中进行更改后, Swift中dispatch_once的新语法是什么?旧版本如下.

var token: dispatch_once_t = 0
func test() {
    dispatch_once(&token) {
    }
}

制作了这些are the changes to libdispatch.

从 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.
网友评论