在Swift 2中,我能够用以下代码创建队列:
let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)
但是这不能在Swift 3中编译。
在Swift 3中,首选的方式是什么?
在Swift 2中,我能够用以下代码创建队列:
let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)
但是这不能在Swift 3中编译。
在Swift 3中,首选的方式是什么?
当前回答
在XCode 8编译,Swift 3 https://github.com/rpthomas/Jedisware
@IBAction func tap(_ sender: AnyObject) {
let thisEmail = "emailaddress.com"
let thisPassword = "myPassword"
DispatchQueue.global(qos: .background).async {
// Validate user input
let result = self.validate(thisEmail, password: thisPassword)
// Go back to the main thread to update the UI
DispatchQueue.main.async {
if !result
{
self.displayFailureAlert()
}
}
}
}
其他回答
在XCode 8编译,Swift 3 https://github.com/rpthomas/Jedisware
@IBAction func tap(_ sender: AnyObject) {
let thisEmail = "emailaddress.com"
let thisPassword = "myPassword"
DispatchQueue.global(qos: .background).async {
// Validate user input
let result = self.validate(thisEmail, password: thisPassword)
// Go back to the main thread to update the UI
DispatchQueue.main.async {
if !result
{
self.displayFailureAlert()
}
}
}
}
现在很简单:
let serialQueue = DispatchQueue(label: "my serial queue")
默认值是serial,要获得concurrent,可以使用可选属性参数.concurrent
你可以在swift 3.0中使用这段代码创建调度队列
DispatchQueue.main.async
{
/*Write your code here*/
}
/* or */
let delayTime = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime)
{
/*Write your code here*/
}
Swift 3
DispatchQueue.main.async {
// Write your code here
}
斯威夫特3
你想在swift代码中调用一些闭包然后你想在storyboard中改变任何类型的改变属于视图,你的应用程序会崩溃
但是你要使用分派方法,你的应用程序不会崩溃
异步方法
DispatchQueue.main.async
{
//Write code here
}
同步方法
DispatchQueue.main.sync
{
//Write code here
}