swift闭包简写

  func calNum(_ n1: Int, _ n2: Int , fn: (Int,Int) -> Int) -> Int {
    return fn(n1,n2)
}

 //1
let res = calNum(10, 100, fn: {
    (v1: Int,v2: Int) -> Int in
    v1 + v2
})
print(res)

 //2
let res1 = calNum(20, 100, fn: {
    v1,v2 -> Int in
    v1 + v2
})
print(res1)
 //3
let res2 = calNum(30, 100, fn: {$0 + $1})
print(res2)

 //4
let res3 = calNum(30, 100, fn: +)
print(res3)

 //5.尾随闭包
let res4 = calNum(40, 50){$0 * $1}
print(res4)

你可能感兴趣的:(swift闭包简写)