将参数传递给 dispatch_async

Passing an argument to dispatch_async

我是 Swift 的新手,正在研究 dispatch_async 函数的工作原理。 API 文档显示 dispatch_async 有两个参数。但是,我可以传递一个参数,这没关系。

dispatch_async(dispatch_get_main_queue()) { 

}

为什么我不需要传入两个参数?

谢谢,

API 文档:

这是一个尾随闭包语法

func someFunctionThatTakesAClosure(closure: () -> ()) {
    // function body goes here
}

// here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure({
    // closure's body goes here
})

// here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
    // trailing closure's body goes here
}

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

这就是 dispatch_async 的样子..

dispatch_async(dispatch_get_main_queue(), ^{

});

这部分

^{

}

是您的函数的第二个参数,它是用于回调实现的匿名代码块。