加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 3 GCD API更改后的dispatch_once

发布时间:2020-12-14 04:26:30 所属栏目:百科 来源:网络整理
导读:在语言版本3中进行更改后,Swift中dispatch_once的新语法是什么?旧版本如下. var token: dispatch_once_t = 0func test() { dispatch_once(token) { }} 制作了这些are the changes to libdispatch. 解决方法 从 doc: Dispatch The free function dispatch_o
在语言版本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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读