Swift 3 GCD API更改后的dispatch_once [英] dispatch_once after the Swift 3 GCD API changes

查看:86
本文介绍了Swift 3 GCD API更改后的dispatch_once的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在语言版本3中进行更改后,Swift中dispatch_once的新语法是什么?旧版本如下.

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) {
    }
}

这些是对libdispatch的更改.

推荐答案

来自 doc :

派遣
免费功能dispatch_once在以下版本中不再可用 迅速.在Swift中,您可以使用延迟初始化的全局变量或静态变量 属性并获得相同的线程安全性和一次调用保证 如dispatch_once提供.示例:

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 3 GCD API更改后的dispatch_once的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆