dispatch_once after the Swift 3 GCD API changes

What is the new syntax for dispatch_once in Swift after the changes made in language version 3? The old version was as follows.

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

These are the changes to libdispatch that were made.


  • From the doc:

    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.