swift--Closed-Packet expression

Source: Internet
Author: User

Closures, which can be understood as a piece of code enclosed in curly braces.

All of the following belong to the closure

Global functions

Nested functions

Closed-Packet expression

Closure expressions can be used to simplify code and make code more concise

1 varnames = ["Mike","John","Mary","Tom","Bill"]2 3Func Sortfun (s1:string, s2:string)Bool4 {5        returnS1 <S26 }7 8Sort (&names, Sortfun)//sort function A method for ordering after a parameter is used, a method of defining the function form9println (names)//Bill, John, Mary, Mike, Tom .

The following closure expressions are used to simplify

Closure Expressions:

1 /* 2 {3       (parameters)-ReturnType in statements4}5 */
1 //1th Step2 3Sort (&names, {(s1:string,s2:string)-Boolinch returnS1 <S2})4 5 //2nd Step6 7Sort (&names,{(S1,S2), Boolinch returnS1 <S2})8 9 //3rd StepTen  OneSort (&names,{(S1,S2)inch returnS1 <S2}) A  - //4th Step -  theSort (&names,{(S1,S2)inchS1 <S2}) -  - //5th Step -  +Sort (&names,{$0< $1}) -  + //6th Step A  atSort (&names, <)

Can eventually be simplified into sort (&names,<) here Swift except for the < cannot be inferred, other values can be inferred based on what the sort compares.

swift--Closed-Packet expression

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.