Swift Language reviews four-closure

Source: Internet
Author: User
Tags closure

Summary: The whole closure function is to simplify the language expression form.

First, closure of the simplification

Closure expression syntax have the following general form:

    • {(), in
    • }

    • Reversednames = names.sorted (by: {(s1:string, s2:string)-Bool in
    • return s1 > S2
    • })

Because The sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type Of the value it returns.

Reversednames = names.sorted (by: {S1, s2 in return s1 > s2})

Implicit Returns from Single-expression Closures

Reversednames = names.sorted (by: {S1, s2 in S1 > s2})

Shorthand Argument Names

Swift automatically provides shorthand argument names to inline closures, which can is used to refer to the values of the Closure ' s arguments by the names $, $, $, and so on.

Reversednames = names.sorted (by: {$ > $})

Operator Methods

Reversednames = names.sorted (by: >)

Second, drag-tail transformation

    • Func somefunctionthattakesaclosure (Closure: ()-Void) {
    • function body goes here
    • }
    • Here's how to call the This function without using a trailing closure:
    • Somefunctionthattakesaclosure (Closure: {
    • Closure ' s body goes here
    • })
    • Here's how to call the This function with a trailing closure instead:
    • Somefunctionthattakesaclosure () {
    • Trailing closure ' s body goes here
    • }

The implementation of closures is outside the list of function parameters;

Streamline again

Reversednames = names.sorted () {$ > $}

Reversednames = names.sorted {$ > $}

Fully removed

    • Let strings = Numbers.map {(number), String in
    • var number = number
    • var output = ""
    • Repeat {
    • Output = digitnames[number% 10]! + Output
    • Number/= 10
    • } while number > 0
    • Return output
    • }

Closures is Reference Types

Whenever assign a function or a closure to a constant or a variable and you are actually setting that constant or variabl E to is a reference to the function or closure.

Escaping Closures

Asynchronous Solutions

Autoclosures

An expression statement

    • Customersinline is ["Ewa", "Barry", "Daniella"]
    • Func Serve (Customer Customerprovider: @autoclosure (), String) {
    • Print ("Now Serving \ (Customerprovider ())!")
    • }
    • Serve (Customer:customersInLine.remove (at:0))
    • Prints "Now Serving ewa!"

Swift Language reviews four-closure

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.