在Swift 2中,我能够用以下代码创建队列:

let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)

但是这不能在Swift 3中编译。

在Swift 3中,首选的方式是什么?


当前回答

斯威夫特3

你想在swift代码中调用一些闭包然后你想在storyboard中改变任何类型的改变属于视图,你的应用程序会崩溃

但是你要使用分派方法,你的应用程序不会崩溃

异步方法

DispatchQueue.main.async 
{
 //Write code here                                   

}

同步方法

DispatchQueue.main.sync 
{
     //Write code here                                  

}

其他回答

我这样做了,如果你想在不被用户注意的情况下刷新你的UI来显示新数据,比如UITableView或UIPickerView,这就特别重要。

    DispatchQueue.main.async
 {
   /*Write your thread code here*/
 }
 let newQueue = DispatchQueue(label: "newname")
 newQueue.sync { 

 // your code

 }

Swift 3

   DispatchQueue.main.async {
        // Write your code here
    }

斯威夫特3

你想在swift代码中调用一些闭包然后你想在storyboard中改变任何类型的改变属于视图,你的应用程序会崩溃

但是你要使用分派方法,你的应用程序不会崩溃

异步方法

DispatchQueue.main.async 
{
 //Write code here                                   

}

同步方法

DispatchQueue.main.sync 
{
     //Write code here                                  

}
DispatchQueue.main.async(execute: {
   // code
})