swift-18 closures and blocks

swift-18 closures and blocks

The swift notes I made in the past were all organized on onenote, and I recently thought of organizing a blog. It is also convenient for you to find it yourself, and you can use it as a document of your own.

Closures are similar to oc blocks but are more widely used than oc blocks

In oc, block is an anonymous function. In swift, function is a special closure. Closure application scenario Asynchronous execution completion callback Inter-controller callback Custom view callback Callback characteristics Call back processing results with parameters The return value is void

Closures are similar to oc blocks, which are executed when needed, can be passed as parameters, can have return values, and need to pay attention to circular references when they include self

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
       //Do any additional setup after loading the view, typically from a nib.