Closures, which can be understood as a piece of code enclosed in curly braces.
All of the following belong to the closure
Global functions
Nested functions
Closed-Packet expression
Closure expressions can be used to simplify code and make code more concise
1 varnames = ["Mike","John","Mary","Tom","Bill"]2 3Func Sortfun (s1:string, s2:string)Bool4 {5 returnS1 <S26 }7 8Sort (&names, Sortfun)//sort function A method for ordering after a parameter is used, a method of defining the function form9println (names)//Bill, John, Mary, Mike, Tom .
The following closure expressions are used to simplify
Closure Expressions:
1 /* 2 {3 (parameters)-ReturnType in statements4}5 */
1 //1th Step2 3Sort (&names, {(s1:string,s2:string)-Boolinch returnS1 <S2})4 5 //2nd Step6 7Sort (&names,{(S1,S2), Boolinch returnS1 <S2})8 9 //3rd StepTen OneSort (&names,{(S1,S2)inch returnS1 <S2}) A - //4th Step - theSort (&names,{(S1,S2)inchS1 <S2}) - - //5th Step - +Sort (&names,{$0< $1}) - + //6th Step A atSort (&names, <)
Can eventually be simplified into sort (&names,<) here Swift except for the < cannot be inferred, other values can be inferred based on what the sort compares.
swift--Closed-Packet expression