将参数传递给dispatch_async

时间:2015-03-19 06:55:46

标签: ios swift ios8 grand-central-dispatch dispatch-async

我是Swift的新手,看看dispatch_async函数是如何工作的。 API文档显示dispatch_async有两个参数。但是,我能够传递一个论点,而且没关系。

dispatch_async(dispatch_get_main_queue()) { 

}

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

谢谢你,

API Doc:

enter image description here

2 个答案:

答案 0 :(得分:2)

这是一个尾随闭包语法

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

答案 1 :(得分:1)

这就是dispatch_async的样子..

dispatch_async(dispatch_get_main_queue(), ^{

});

此部分

^{

}

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