Swift function closure

Source: Internet
Author: User

First, closure of the concept


There are 2 prerequisites for a computer language to support closures:

1, Support function type, can pass function as parameter or return value;

2. Support function nesting


Example:


Func caculate (opr:string)-(int,int)->int {

var result: (Int,int)->int

Switch (OPR) {

Case "+":

result={(A:int,b:int)->int in

Return a+b

}

Default

result = {(A:int,b:int), Int in

Return A-B

}


}

return result;

}


Let F1: (int,int)->int = caculate ("+")


println (F1 (2,5))


From the example above, we can give Swift a definition of closure: a closure is a self-contained anonymous function block, which can be used as an expression, function parameter and function return value, the result of the operation of the closure expression is a type of function


Swift closures are similar to C and Obeject-c code libraries, C + + and C # Lamba expressions, Internal anonymous classes in Jave



Two, the expression of closure


The standard notation for closure expressions in Swift is as follows:


{(parameter list), return value type in statement group}


1. Simplification of type inference

In swift, parameter types and parameter return values can be inferred from the context environment, and 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, the return value is of type int and the simplified form is as follows:


{b In return a + b}



2. Hide return keyword

If there is only one statement in the inner statement group of the closure, such as Rerturn A + B, then this statement is a return statement, the preceding keyword Rerturn can be omitted, the ellipsis form is as follows:


{A-B in A + B}



3. Abbreviation parameter name


Swift provides the function of the abbreviation of the parameter name, we can use $0,$1,$2 to represent the function in the call closure, the number of the first argument, the second argument, and so on


Func caculate (opr:string)-(int,int)->int {

var result: (Int,int)->int

Swtich opr{

Case "+":

RESULT={$1,$2}

Default

RESULT={$1-$2}

}

return result

}



4. Use closure return value


A closure expression is essentially a function type and has a return value, and you can use the return value of the closure directly in an expression


Let C1:int = {(a:int,b:int)->int in return a+b} (10,5)

println (C1)


Iii. use of trailing closures


A closure expression can be passed as a function parameter, and the closure must be the last parameter to use


Example:

Func caculate (OPR:STRING,FUNCN: (int,int)->int) {switch opr{case "+": PR Intln (FuncN (10,5)) default:println (FuncN (10,5))}} caculate ("+") {(a:int,b:int)->in T in return a + B} caculate ("-") {(A:int,b:int)->int in return a-c}



Iv. constants and variables in the capture context


A nested function or closure can access variables and constants in its context, a process called capturing values, even if the original fields that define those variables and constants do not already exist, and nested functions or closures can still reference and modify these values within the body of the function or the closure.


Example:

Funmakearray ()->string-[string]{var ary:[string] = [String] () funcaddelement (element:string) ->[string]{ary.append (Element) return ary} return addelement} Let F1 = Fun Makearray () println (F1 ("Zhang San")) println (F1 ("John Doe")))


This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1620594

Swift function closure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.