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.
Let's look at a sample code:
func calculate (Opr:string, funn: (Int,Int)-> Int) {///last parameter Funn is (int,int)-> int function type, Funn can receive the closure expression switch (OPR) { case "+" : print ("10 + 5 =\ (Funn (10,5)) default: Print ("10 - 5 =\ (Funn (10,5))") }} calculate ("+", 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 ("+ ") {$0 + $1 } //called, which is the trailing closure
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
650) this.width=650; "title=" 00.png "alt=" Wkiol1bvzwcy3laiaaas2mbeznc895.png "src=" http://s2.51cto.com/wyfs02/M00/ 7c/ab/wkiol1bvzwcy3laiaaas2mbeznc895.png "/>
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
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746417
Learning Swift from scratch (day 23)--trailing closures