Preface
Closures are functional self-contained modules that can be passed and used in code. Closures in Swift are similar to blocks in C and Objective-C and lambdas in some other programming languages. The following article introduces the closures in Swift 3.0 in detail. If you are interested, let’s take a look.
start
The writing format of the closure is as follows:
{ (parameters) -> return type in statements }
like
reversedNames = (by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )
When used, it can be simplified to
reversedNames = (by: { s1, s2 in return s1 > s2 } )
It can also be simplified to
reversedNames = (by: { s1, s2 in s1 > s2 } )
It can even be simplified to
reversedNames = (by: { $0 > $1 } )
$0 is the first parameter, and so on
If you return a Boolean value, you can directly give a judgment symbol, such as
reversedNames = (by: >)
Tail closure
reversedNames = () { $0 > $1 }
Or (without other parameters)
reversedNames = { $0 > $1 }
Both methods are OK
Capture value
The following code, the closure can obtain and modify the variables around it
func makeIncrementer(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementer() -> Int { runningTotal += amount return runningTotal } return incrementer }
The above function returns a closure, which adds the external variable runningTotal to read and modify the closure.
let incrementByTen = makeIncrementer(forIncrement: 10) incrementByTen() // returns a value of 10 incrementByTen() // returns a value of 20 incrementByTen() // returns a value of 30
Closure reference type
The closure can be referenced in this way and called:
let alsoIncrementByTen = incrementByTen alsoIncrementByTen()
@escaping
If the closure passed to the function is not called in the function, but saves the current closure with external variables in the function and then calls it at the appropriate time, it is necessary to add the @escaping keyword before the closure parameter, otherwise the compiler will report an error.
What is easier to understand is the frequently used closure that is executed only after the request is completed.
Official examples are as follows:
//A situation where @escaping is requiredvar completionHandlers: [() -> Void] = [] func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) { (completionHandler) } //Can you directly call the incoming closure inside the function without @escapingfunc someFunctionWithNonescapingClosure(closure: () -> Void) { closure() }
@autoclosure automatic closure
// customersInLine is ["Alex", "Ewa", "Barry", "Daniella"] func serve(customer customerProvider: () -> String) { print("Now serving \(customerProvider())!") } serve(customer: { (at: 0) } ) // Prints "Now serving Alex!"
As shown in the above code, we add a closure with the return type String, and we need to add {} outside. For the convenience of writing, add the @autoclosure keyword, so this pair of {} can be omitted.
// customersInLine is ["Ewa", "Barry", "Daniella"] func serve(customer customerProvider: @autoclosure () -> String) { print("Now serving \(customerProvider())!") } serve(customer: (at: 0)) // Prints "Now serving Ewa!"
The compiler will help us mark this line of code as a closure. This code will not be called immediately, but will be called when it is called as a closure in the function.
Refer to the original English text:
/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/#//apple_ref/doc/uid/TP40014097-CH11-ID94
Summarize
The above is the entire content of this article. I hope that the content of this article will be of some help to everyone’s learning or using Swift. If you have any questions, you can leave a message to communicate. Thank you for your support.