Swift Closure Beginner's explanation (novice must SEE)

Source: Internet
Author: User
Tags closure gcd

Meaning of closures

closures are self-contained function code blocks that can be passed and used in code. Closures in Swift are similar to the code blocks in C and objective-c (blocks) and anonymous functions in some other programming languages.

Closures can capture and store references to any constants and variables in the context in which they are located. is called a package constant and variable. Swift will manage all the memory operations involved in the capture process for you.

Main optimization of closures

    • Infer parameters and return value types with context
    • Implicitly returns a single-expression closure, where a single-expression closure can omit a return keyword
    • Parameter name abbreviation
    • Trailing closure syntax

Closure application Scenarios

The application scenario for closures and blocks is the same. (When you think this is possible with block in OC, it can be implemented in swift with closures)

1, executes the completion callback asynchronously.

2. Callback between controllers

3, Custom View callback

Three modes of closures

Closures are available in three forms, 1, with no parameters and no return value for closures (simplest closures)

Here is a point to note, be sure to execute the call closure, or the inside code will not execute

2, closures with no return value for parameters

3, closure with parameters with return value

It is important to note that the return value needs to be manipulated or a warning will be reported.

Attention!

In order to distinguish the definition and implementation of the function, it is said that the popular point is the segmentation of the partition and subsequent operations!!!

Closure callback

A closure callback, which means that the closure is invoked as a parameter.

It is important to note that the GCD in swift and the GCD in OC are different.

Trailing closures

The trailing closure is a relatively concise notation.

It is important to note that, "most", the general closure system will automatically help us to do the trailing closure, not all, such as the top gcd nested.

Then we modify it according to the meaning of the trailing closure on the top. Delete the last parameter of the callback and the outermost parenthesis.

Escape Closure Package

When a closure is passed as a parameter to a function, but the closure is executed after the function is returned, we call the closure escaping from the function. When you define a function that accepts a closure as a parameter, you can label it before the parameter name @escaping to indicate that the closure is allowed to "escape" the function.

One way to make a closure "escape" out of a function is to keep the closure in a variable defined outside the function. For example, many functions that initiate asynchronous operations accept a closure parameter as completion handler. Such functions return immediately after the asynchronous operation begins, but the closure is not invoked until the end of the asynchronous operation. In this case, the closure needs to "escape" out of the function, because the closure needs to be called after the function is returned. For example:

var completionHandlers: [() -> Void] = []func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) { completionHandlers.append(completionHandler)}

 

Novice share, do not like to spray!!! Seek to share, to seek for diffusion. Forwarding.

Swift Closure Beginner's explanation (novice must SEE)

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.