我读过this一个,但对我来说没有任何理解。但我理解这种闭包语法:
var cal = {(num1: Int)-> Int in
return num1 * 2;
}
var clusers = [cal,
{(num1:Int) -> Int in return num1 * 3},
{(num1:Int) -> Int in num1 * 4},
{(num1:Int) in num1 * 5},
{ num1 in num1 * 6},
{ $0 * 7}]
for cluser in clusers{
cluser(100)
}
如何进行尾随关闭?
基本上这是一个尾随闭包。我无法理解:
//call dispatch async to send a closure to download queue
dispatch_async(download) { () ->Void in
//some code goes here
}
答案 0 :(得分:1)
让这是我的功能
func someFunctionThatTakesAClosure(index: Int, closure: () -> Void) {
}
如果你需要将一个闭包传递给上面的函数,你可以写下面的内容
someFunctionThatTakesAClosure(5, closure: {
// code included in closure
})
但是如果闭包是传递给函数的最后一个参数,就像在上面的函数中一样,我们可以像上面这样编写一段代码
someFunctionThatTakesAClosure(5) {
// code included in closure
}
这就是为什么它被称为尾随闭包。