Ios Swift中的超简单尾随闭包语法

Ios Swift中的超简单尾随闭包语法,ios,swift,closures,trailing,Ios,Swift,Closures,Trailing,我正试着遵循一个尾随关闭的规则 这就是功能: func someFunctionThatTakesAClosure(closure: () -> Void) { // function body goes here print("we do something here and then go back")//does not print } 我把它叫做这里 print("about to call function")//prints ok

我正试着遵循一个尾随关闭的规则

这就是功能:

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
    print("we do something here and then go back")//does not print
}
我把它叫做这里

        print("about to call function")//prints ok
        someFunctionThatTakesAClosure(closure: {
            print("we did what was in the function and can now do something else")//does not print
        })
        print("after calling function")//prints ok
但是,没有调用该函数。上面的问题是什么

以下是苹果的例子:

func somefunction接受闭包(闭包:()->Void){ //函数体在这里}

//下面是在不使用尾随闭包的情况下调用此函数的方法:

一些需要关闭的函数(关闭:{ //闭包的主体在这里})


您需要的文档解释不是很清楚

print("1")
someFunctionThatTakesAClosure() {  // can be also  someFunctionThatTakesAClosure { without ()
    print("3") 

}

尾随闭包用于完成,例如当您执行网络请求并最终返回如下响应时

func someFunctionThatTakesAClosure(completion:  @escaping ([String]) -> Void) { 
   print("inside the function body") 
   Api.getData { 
      completion(arr)
   }
}  
打电话

print("Before calling the function")
someFunctionThatTakesAClosure { (arr) in
  print("Inside the function callback  / trailing closure " , arr)
}
print("After calling the function") 
你错过了什么


您需要的文档解释不是很清楚

print("1")
someFunctionThatTakesAClosure() {  // can be also  someFunctionThatTakesAClosure { without ()
    print("3") 

}

尾随闭包用于完成,例如当您执行网络请求并最终返回如下响应时

func someFunctionThatTakesAClosure(completion:  @escaping ([String]) -> Void) { 
   print("inside the function body") 
   Api.getData { 
      completion(arr)
   }
}  
打电话

print("Before calling the function")
someFunctionThatTakesAClosure { (arr) in
  print("Inside the function callback  / trailing closure " , arr)
}
print("After calling the function") 
你错过了什么


以下是您的示例:

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
    print("we do something here and then go back")

    // don't forget to call the closure
    closure()
}


print("about to call function")

// call the function using trailing closure syntax
someFunctionThatTakesAClosure() {
    print("we did what was in the function and can now do something else")
}

print("after calling function")
输出:


以下是您的示例:

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
    print("we do something here and then go back")

    // don't forget to call the closure
    closure()
}


print("about to call function")

// call the function using trailing closure syntax
someFunctionThatTakesAClosure() {
    print("we did what was in the function and can now do something else")
}

print("after calling function")
输出:


闭包只是一个值。就像任何其他价值一样,你可以“忽略”它的存在,什么也不会发生。使任何事情发生都需要实际使用该值。闭包只是一个值。就像任何其他价值一样,你可以“忽略”它的存在,什么也不会发生。让任何事情发生都需要你实际使用这个值。谢谢,我看到了,但读起来好像你可以使用任何一个一样。谢谢澄清!我对答案投了赞成票。谢谢,我看到了,但我读了它,就好像你们可以用任何一个一样。谢谢澄清!我对答案投了赞成票。