美文网首页
swift闭包简写

swift闭包简写

作者: Mr_HeH | 来源:发表于2020-09-15 14:24 被阅读0次
      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闭包简写

          本文链接:https://www.haomeiwen.com/subject/wjkfyktx.html