Today I will share with you the usage of closures in swift. I personally think closures are short for functions. If you are not familiar with functions, please refer to the Quick Start article-Functions
1: Function Type
Var variable of function type: (type)-> return value = Function Name Description: 1: function name indicates function address 2: (type)-> return value indicates Function Type 3: the function type must be consistent with the parameter and return value of the function name.
Example
/* 1: func is the function keyword. 2: Say is the function name, indicating the function address.
3: The Say function parameter is an integer and returns a Boolean value.
*/
Func Say (num: Int)-> Bool {
Return num> 10
}
Var By :( Int)-> Bool = Say
/*
1: Say is the address of the function name, and the function type. The parameter is an integer, And the return value is a boolean type.
2: (Int)-> Bool indicates the function type, the parameter is an integer, And the return value is a boolean type.
That is, By is a function variable (the parameter is an integer, and the returned value is a boolean type) pointing to the function name (Say)
*/
Var B = By (12) // by (12) directly call the Function
Println (B)
Running result
True
2: Closure format
Format: {(parameter: type)-> return type in execution method return type}
Note:
1; closure mainly points to the Function Type
2: The closure parameters must be consistent with the function type parameters and return values.
Example
/* Function Getlist description parameter: 1: first parameter Integer Set variable 2: second parameter, function type variable parameter is Integer return value Boolean return value Integer Set */func GetList (arr: int [], pre :( Int)-> Bool)-> Int [] {// defines an empty variable Integer Set var tempArr = Int [] () for temp in arr {if pre (temp) {tempArr + = temp;} return tempArr;}/* Call Getlist to describe the first parameter integer array [1, 2, 3, 4] The second parameter closure points to the function type. {(S) in return s> 2} closure type description. The parameter is an integer and the return value is a boolean type */let arr = GetList ([,], {(s: int) in return s> 2}) println (arr) running result [3, 4]
3: Closure shorthand Method
1; abbreviation 1: omitting the parameter type and brackets
2: Abbreviation 2: omitting the parameter type, Parentheses, and return keyword
3: abbreviated parameter name ($0 represents the first parameter, and $1 represents the second parameter)
/* Function Getlist description parameter: 1: first parameter Integer Set variable 2: second parameter, function type variable parameter is Integer return value Boolean return value Integer Set */func GetList (arr: int [], pre :( Int)-> Bool)-> Int [] {// defines an empty variable Integer Set var tempArr = Int [] () for temp in arr {if pre (temp) {tempArr + = temp;} return tempArr;} // The first Abbreviation: omit parameter types and brackets var Arr = GetList ([1, 2, 3, 4], {s in return s> 2}) // The second Abbreviation: Omit parameter types and parentheses, return keyword Arr = GetList ([1, 2, 3, 4], {s in s> 2}) // The third Abbreviation: Arr = GetList ([1, 2, 3, 4], {$0> 2}) // $0 indicates the first println (Arr) parameter. The running result of the preceding three operations is [3, 4].
4: trailing Closure
Official definition: a trailing closure is a closure expression written after the function brackets. The function supports calling it as the last parameter.
/* 1: function Fuction Parameter Function Type 2: The parameter is a function type, function type, parameter is null, return value is null */func Fuction (closure :() -> () {println ("Korean type without parameters and return values")} // ----------------- method 1 ------------/* Call function Fuction1: the parameter is closure 2: the closure parameter is null, and the returned value is null */Fuction ({}) // ----------------- The second call method ------------ Fuction () {} the running results of the above two call methods are all Korean type with no parameters and no return values
In the subsequent articles, I wrote the swift language I learned to form a series. Because it is a new language, it is inevitable that there are deficiencies. Please give me your comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.
Summary. Send a mind map to end the article