Swift 2.0 Study Notes (day 23)--trailing closures

Source: Internet
Author: User

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

Related Article

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.