Today is mainly to share with you is the use of closure in Swift, I personally think the closure is a shorthand method of functions, if you are not very familiar with the function of the swift Introduction-function
1: Function type
function type var variable:(type) return value = function Name Description: 1: Function name denotes function address 2: (type)--return value denotes function Type 3: function type must be consistent with parameter and return value of function name
Example
/*1:func is the function keyword 2:say is the function name, which represents the address of the function
The 3:say function parameter is an integer that returns a Boolean type
*/
return num > 10
}
1:say is the address of the function name, and the function type, parameter is shaping, return value Boolean type
var B = by (//by) (12) Call function directly
Run results
True
2: Closure format
Format: { (parameter: type), return type in execution method return type }
Description
1. Closures mainly refer to function types
2: The parameters of the closure must match the parameters and return values of the function type
Example
/* Function GetList description parameter: 1: First parameter shaping set Variable 2: Second parameter, function type variable parameter for reshape return value Boolean type return value shaping set */func GetList (arr:int[], pre: (Int )->bool)->int[]{ //define an empty variable shaping set var Temparr = int[] () for temp in arr { if pre (temp) { Temparr + = temp; } } return Temparr;} /* Call GetList Description The first parameter shaping array [1,2,3,4] The second parameter closure to point to the function type. {(s) in return s>2} closure type description, parameter is shaped, return value is Boolean type */let arr=getlist ([1,2,3,4],{(S:int) in return s>2}) println (arr) run result [3, 4]
3: Closure Shorthand method
1; First shorthand: Omitting parameter types and parentheses
2: Second shorthand: Omitting parameter types and parentheses, return keyword
3: The third shorthand: parameter name abbreviation (the first parameter is represented by $ A, and the second parameter is represented by $ A)
/* Function GetList description parameter: 1: First parameter shaping set Variable 2: Second parameter, function type variable parameter for reshape return value Boolean type return value shaping set */func GetList (arr:int[], pre: (INT)- >bool)->int[]{ //define an empty variable shaping set var Temparr = int[] () for temp in arr { if pre (temp) { Temparr + = temp; } } return Temparr;} First shorthand: Omitting parameter types and parentheses var arr = GetList ([1,2,3,4],{s in return s>2})//second shorthand: Omit parameter type and parentheses, return keyword arr = GetList ([1,2,3,4], {s in s>2}) The third shorthand: the parameter name abbreviation ARR = GetList ([1,2,3,4],{$0>2})//Where the number of the first parameter println (arr) is the result of a run of 3 usages [3, 4]
4: Trailing closures
Official definition: A trailing closure is a closure expression that is written after the function bracket, and the function supports calling it as the last argument
/*1: Function fuction parameter function Type 2: parameter is function type, function type, parameter is NULL, return value is null */func fuction (closure: ()) { println ("No parameter, no return value of Korean type")}// -----------------the first calling method------------/* Call the function Fuction1: The parameter is closure 2: The parameter of the closure is empty, the return value is empty */fuction ({})//----------------- The second method of calling------------Fuction () {} The results of the 2 calling methods above are no parameters, Korean type with no return value
later in the article, I went back to learn from the Swift language of knowledge written to form a series. Since it is a new language, it is inevitable that there are deficiencies, and you are welcome to advise me. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply
Finally, a summary. Send the mind map as the end of the article
Introduction to Swift-closures and functions