original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
A closure expression can be passed as a function parameter, and if the closure expression is long, it affects the readability of the program. A trailing closure is a closure expression that is written after the function brackets, and the function supports calling it as the last argument.
Func Calculate (opr:string, Funn: (int, int) and int) {///last parameter Funn is (int,int)-int function type, Funn can receive closure expression switch ( OPR) {case "+": print ("ten + 5 = \ (Funn (10,5))") default: print ("10-5 = \ (Funn (10,5))") }} Calculat E ("+", Funn: {(A:int, b:int), int in return a + B}) //Call Calculate ("+") {(A:int, b:int), int in return a + B}//Call, this form is the trailing closure calculate ("+") {$ + $} //Call, this form is the trailing closure package
It is important to note that closures must be the last parameter of the parameter list, if Calculate functions are defined in the following form:
func Calculate (Funn: (Int, Int) ->int, opr:string) {
...
}
Since the closure expression is not the last argument, the call Calculate functions cannot be written using trailing closures.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift 2.0 Study Notes (day 23)--trailing closures