代码之家  ›  专栏  ›  技术社区  ›  Mochi

为什么闭包的实际主体永远不会被执行?

  •  0
  • Mochi  · 技术社区  · 5 年前

    来自苹果的Swift编程语言(Swift 5.2beta)

    func someFunctionThatTakesAClosure(closure: () -> Void) {
        // function body goes here
           print("Declaring Function")
    }
    
    // Here's how you call this function without using a trailing closure:
    
    someFunctionThatTakesAClosure(closure: {
        // closure's body goes here
           print("Without Trailing Closure Syntax")
    })
    
    // Here's how you call this function with a trailing closure instead:
    
    someFunctionThatTakesAClosure() {
        // trailing closure's body goes here
           print("Trailing Closure Syntax")
    }
    

    在这些例子中,当 someFunctionThatTakesAClosure

    1 回复  |  直到 5 年前
        1
  •  2
  •   AdamPro13    5 年前

    您需要更改为:

    func someFunctionThatTakesAClosure(closure: () -> Void) {
       print("Declaring Function")
       closure()
    }