the closure expressions in Swift are flexible and have the following standard syntax format:
{(parameter list), return value type in
Statement Group
}
Where the argument list is the same as the parameter list in the function, the return value type is similar to the return value type in the function, but the difference is that the IN keyword is followed.
Swift offers a variety of closure simplification methods, which we will cover in several different forms.
1. Type inference Simplification
Type inference is the strength of swift, and swift can infer parameter types and return value types based on the context environment. The following code is a standard form of closure:
{(A:int, b:int), Int in
Return a + b
}
Swift can infer that the parameters A and B are of type int, and the return value is also of type int. The simplified form is as follows:
{A, B in return a + b}
The following example code is modified using this simplified method:
Func Calculate (opr:string)-(int,int), int { var result: (int,int), int switch (OPR) {case "+" : result = {A, b in return a + B}① default: result = {A, b in return a-}② } retur n result; Let F1: (int,int)-int = calculate ("+") println ("ten + 5 = \ (f1 (10,5))") Let F2: (int,int), int = calculate ("-") println ("ten + 5 = \ (F2 (10,5))")
The closure of lines ① and ② of the preceding code is a simplified notation of the previous example, where A and B are parameters, and return is followed by the returned value. What do you think? Very simple, huh?
2. Hide the return keyword
If the statement group has only one statement in the inner closure, such as return a + B, then the statement is a return statement. The preceding keyword return can be omitted and omitted in the following form:
{A, b in A + B}
The following example code is modified using this simplified method:
Func Calculate (opr:string)-(int,int), int { var result: (int,int), int switch (OPR) {case "+" : result = {A, b in return a + B}① default: result = {A, b in return a-}② } retur n result;
The closure return keyword for line ① and line ② above is omitted, and it is important to note that the omission is only one return statement in the closure. There are several statements below that are not allowed.
{A, b in Var C; a + b}
3. Abbreviation parameter name
The closure expressions described in the previous section are simple enough, but swift closures can be simplified. Swift provides the parameter name abbreviation function, we can use $, $, $ to represent the parameters of the call closure, "$" refers to the first parameter, "the second parameter," $ refers to the third parameter, and so on $n+1 refers to the nth parameter.
With the parameter name abbreviation, you can also omit the definition of the parameter list in the closure, and Swift can infer the type of these abbreviated parameters. In addition, the In keyword can also be omitted. The parameter name is abbreviated as follows:
{$ + $}
The sample code modified with the parameter name abbreviation is as follows:
Func Calculate (opr:string)-(int,int), int { var result: (int,int), int switch (OPR) {case "+" : result = {$ + $1}① default: result = {$-$1}② } return result; Let F1: (int,int)-int = calculate ("+") println ("ten + 5 = \ (f1 (10,5))") Let F2: (int,int), int = calculate ("-") println ("ten + 5 = \ (F2 (10,5))")
The closure of line ① and line ② of the above code takes the parameter name abbreviation.
4. Using the closure return value
The closure expression is essentially a function type and has a return value, and we can use the return value of the closure directly in the expression. To re-modify the add and sub closures, the sample code is as follows:
Let C1:int = {(A:int, b:int)-Int in return a + b } (10,5) ①println ("ten + 5 = \ (C1)") Let C2:int = {(a:int , B:int), Int in return a -B} (10,5) ②println ("10-5 = \ (C2)")
The code above has two expressions, and the ① line code assigns a value to C1, followed by a closure expression. However, the closure expression cannot be assigned directly to C1 because C1 is of type int and the return value of the closure is required. This requires that a pair of parentheses (10,5) be followed by the curly braces at the end of the closure, with the parentheses (10,5) passing the parameters for the closure. The same is true for line ② code. This method allows you to assign values directly to variables and constants, which is convenient for use in some scenarios.
For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift uses closure expressions