What is trailing closure syntax in Swift?

前端 未结 4 1650
执笔经年
执笔经年 2020-12-06 23:12

Swift\'s documentation on closures states:

Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-fre

相关标签:
4条回答
  • 2020-12-06 23:33

    A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.

    func doSomething(number:Int, onSuccess closure:(Int)->Void) {
    
        closure(number * number * number)
    
    }
    
    doSomething(number: 100) { (numberCube) in
    
        print(numberCube) // prints  1000000
    
    }
    

    The argument label onSuccess is not there in the function call. Even though the closure is included in the function parameters list, swift will take it out of the parameter block to make the code more readable.

    0 讨论(0)
  • 2020-12-06 23:40

    If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.

     func funcWithATrailingClosure(closure: () -> Void) {
    // function body goes here
     }
    
    // Here's how you call this function without using a trailing closure:
    funcWithATrailingClosure(closure: {
    // closure's body goes here
    })
    
    // Here's how you call this function with a trailing closure instead:
    funcWithATrailingClosure() {
    // trailing closure's body goes here
    }
    
    0 讨论(0)
  • 2020-12-06 23:43
    1. A closure expression that is written outside of (and after) the parentheses of the function call it supports

    It is just syntactic sugar to write less and is easier to read.

    Giving you a use case:

    You have a function that needs a another function (or closure) as a parameter like that:

    func fooFunc(paramfunc: () -> Void) {
        paramfunc();
    }
    

    Calling the function and giving a function as an argument is normal. Giving it a closure means the argument you give is a nameless function written between {} and with the type signature in the beginning (it is an anonymous function).

    While calling a function which needs a function as argument and writing the closure after the parenthesis or omitting the parenthesis if it is the only argument (multiple trailing closures coming in Swift 5.3) makes it trailing closure syntax.

    fooFunc { () -> Void in
        print("Bar");
    }
    
    fooFunc() { () -> Void in
        print("Bar");
    }
    

    The parenthesis after the function name can even be omitted.

    0 讨论(0)
  • 2020-12-06 23:48

    Trailing Closures

    If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.

    https://docs.swift.org/swift-book/LanguageGuide/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID102

    func someFunctionThatTakesAClosure(closure: () -> Void) {
        // function body goes here
    }
    
    // Here's how you call this function without using a trailing closure:
    
    someFunctionThatTakesAClosure(closure: {
        // closure's body goes here
    })
    
    // Here's how you call this function with a trailing closure instead:
    
    someFunctionThatTakesAClosure() {
        // trailing closure's body goes here
    }
    

    If a closure expression is provided as the function or method’s only argument and you provide that expression as a trailing closure, you do not need to write a pair of parentheses () after the function or method’s name when you call the function:

    reversedNames = names.sorted { $0 > $1 }
    
    0 讨论(0)
提交回复
热议问题