Directory:
- Basic syntax
- Trailing closures
- Value capture
Closures are self-contained function code blocks, and closures take one of the following three forms:
? A global function is a closed packet that has a name but does not capture any value
? A nested function is a closure that has a name and can capture values within its enclosing function domain
? A closure expression is an anonymous closure that uses lightweight syntax to capture variables or constant values in its context.
Using a concise closure expression has the following advantages:
? Use context to infer parameters and return value types
? Implicitly returns a single-expression closure, where a single-expression closure can omit the return keyword
? parameter name abbreviation
? Trailing closure syntax
Both functions and closures are reference types.
Basic syntax
inch Statements}
(1). The closure expression parameter can be a in-out parameter, but you cannot set a default value
(2). The closure expression parameter can use a variable parameter with a specific name
(3). You can use tuples as parameters and return values for a closure expression
//Inline ClosuresReversednames = names.sorted (by: {(s1:string, s2:string), Boolinchreturn S1>S2})//Shorthand MethodReversednames = names.sorted (by: {(s1:string, s2:string), Boolinchreturn s1 >S2})//when an inline closure expression constructs a closure that is passed as a parameter to a function or method, it is always possible to infer the closure's parameters and return value types. //This means that when a closure is a function or a method parameter, an inline closure can be constructed without a full formatReversednames = names.sorted (by: {S1, s2inchreturn s1 >S2})//single-line expression closures can implicitly return the result of a single-line expression by omitting the return keywordReversednames = names.sorted (by: {S1, s2inchS1 >S2})//Swift automatically provides parameter name abbreviations for inline closures, and can use $0,$1,$2 to sequentially invoke closure parametersReversednames = names.sorted (by: {$0> $1 } )//operator method, the string operator > just receives two string arguments to return a bool result, which can be abbreviated asReversednames = names.sorted (by: >)
Trailing closures
If the closure expression is the last parameter of the function, you can enhance the readability of the function by using a trailing closure that is written outside the function brackets to omit the label of the enclosing expression parameter.
Func somefunctionthattakesaclosure (Closure: ()Void) { //function Body Part}//inline closures for function callssomefunctionthattakesaclosure (closure: {//Closure Body part})//trailing closures for function callssomefunctionthattakesaclosure () {//Closure Body part}reversednames= Names.sorted () {$0> $1 }//If a closure expression is a unique parameter to a function or method, you can omit () when you use a trailing closureReversednames = names.sorted {$0> $1}
Value capture
Closures can capture constants or variables in the context in which they are defined. Even if the original scope for defining these constants and variables no longer exists, the closures can still reference and modify these values in the closure function body.
// after Makeincrementer execution, the scope of the runningtotal does not exist, // but the return function is still able to change and access the value of RunningTotal. func makeincrementer (forincrement amount:int), Int { /c4>0 - Int { + = amount return runningtotal } Ten )// closures are reference types, with closures assigned to two variables or constants, which point to the same closure let Alsoincrementbyten = Incrementbyten
Statement: This series of content is from the network or electronic books, only to do learning summary!
Swift Learning Note (8): Closures