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

Source: Internet
Author: User
Tags closure

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

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.