Swift_4 _ closure (Blocks)

Source: Internet
Author: User

Swift_4 _ closure (Blocks)

Import Foundationprintln ("Hello, World! ") Var arr = [,] func hasClosure (list: [Int], v2: Int, cb :( num: Int, v3: Int)-> Bool) -> Bool {for item in arr {if (cb (num: item, v3: v2) {return true ;}return false ;}var v2 = hasClosure (arr, 82, {(num: Int, v3: Int)-> Bool in return num> = v3;}); println ("v2 is \ (v2 )") // The sort function sorts the values in an array of known types based on the sort closure you provide. Once sorting is completed, the function returns a new array of the same size as the original array, which contains the same type of elements that have been correctly sorted. let names = ["Chris", "Alex ", "Ewa", "Barry", "Daniella"] func backwards (s1: String, s2: String)-> Bool {return s1> s2} var reversed = sort (names, backwards) // reversed is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"] // closure version reversed = sort (names, {(s1: String, s2: String)-> Bool in return s1> s2}) // single row reversed = sort (names, {(s1: String, s2: String) -> Bool in return s1> s2}) // reversed = sort (names, {s1, s2 in s1> s2}) // reversed = sort (names, {$0> $1}) // ultimate simplified reversed = sort (names,>) println ("reversed is \ (reversed )") // closure expression // {// (parameters)-> returnType in // statements //}


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.