相关疑难解决方法(0)

等到任务完成

我怎么能让我的代码等到DispatchQueue中的任务完成?它需要任何CompletionHandler或其他东西吗?

func myFunction() {
    var a: Int?

    DispatchQueue.main.async {
        var b: Int = 3
        a = b
    }

    // wait until the task finishes, then print 

    print(a) // - this will contain nil, of course, because it
             // will execute before the code above

}
Run Code Online (Sandbox Code Playgroud)

我正在使用Xcode 8.2并在Swift 3中编写.

multithreading asynchronous grand-central-dispatch swift swift3

80
推荐指数
3
解决办法
8万
查看次数