Basic syntax for closures

Source: Internet
Author: User

: Playground-noun:a place where people can playimport uikit//initialize an array of integers var arr = [1, 3, 5, 7, 8, 10, 12, 2, 0, 11, 9] Sort, default is from small to large sort arr.sort ()//through closures, custom sort method Arr.sort ({(A:int, b:int), Bool in    return a > B})//Initialize an array of strings Var a RR2 = ["AA", "B", "BB", "ABCD", "AABBCC", "CDEFG", "a", "C"]//sort, sort by string length, if the length is equal, compare two strings Arr2.sort ({string1:string, string2:string), Bool in let        string1length = string1.characters.count let    string2lenght = String2.characters.count        if string1length! = string2lenght {//Length unequal        return String1length < string2lenght< c5/>} else {                            //length equal        return string1 < string2    }})

The following is a simple syntax for closing a packet :

{( parameter )- return type in function body }

Basic syntax for closures

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.