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) {//The last parameter, Funn, is the (int,int)-Int function type, Funn can receive a closure expression Switch(OPR) { Case "+": Print ("+ 5 = \ (Funn (10,5))") default: Print ("10-5 = \ (Funn (10,5))")}} calculate ("+", Funn: {(A:int, b:int), Intinch returnA + b})//calledCalculate ("+") {(A:int, b:int), Intinch returnA + b}//called, this form is the trailing closureCalculate ("+") { $0+ $1}//called, this form is the trailing closure
It should be noted that the closure must be the last parameter of the parameter list if the Calculate function is defined in the following form:
Func Calculate (funn: (int, int), int, opr:string) {
...
}
Because the closure expression is not the last parameter, the call to the Calculate function cannot be written using a trailing closure.
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 Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Swift 2.0 Study Notes (day 23)--trailing closures