SWIFT 3 GCD API Changed DISPATCH_ONCE

After the changes in language version 3, what is the new syntax for dispatch_once in Swift? The old version is as follows.

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

These are the changes to libdispatch.

From the 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.

In language version 3 After making changes in Swift, what is the new syntax of dispatch_once in Swift? The old version is as follows.

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

These are the changes to libdispatch.

From doc:

< p>

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.

Leave a Comment

Your email address will not be published.