Swift 5 Concise Guide closure Closure

System: Mac OS 10.15.2, XCode 11.3, swift 5.0
Writing Time: 2020-01-09

Explanation

Swift closures (Closure), equivalent to the position of closure of Block OC!

Objc closure wording

 void (^printBlock)(NSString *x);  
      printBlock = ^(NSString* str)  { 
           NSLog(@"print:%@", str);  
      };  
      printBlock(@"hello world!"); 

Closure Swift wording

// 定义语法
//{  
//   (参数列表) ->返回值类型 in 
//     语句组
//}

  // 声明一个闭包(有两个整形参数,且返回值为整形的闭包)
    var sumClosure:((a: Int, b: Int) -> Int)
    
    // 实现闭包
    sumClosure = {  (a: Int, b: Int) -> Int in 
         return a + b
    }

    // 调用
    let sum = sumClosure(a: 10,b: 20)print(sum)

Swift closure redefined

// 有参数无返回值的
 typealias Myclosure1 = (str:String) ->Void

// 有参数无返回值的
typealias Myclosure2=(str:String) ->String

// 两个参数,一个返回值
typealias Myclosure3=(str:String,str:String)->String

// 无参数,无返回值
typealias Myclosure4=()->Void

// 使用如下
 var closure1: Myclosure1?
   closure1 = { (str: String) ->Void in 
        print(str)
    } 
   closure1!(str: "HelloWorld")

Comparison of the method as a parameter Swift

// MARK: - 闭包作为方法参数
 var closure4:Myclosure4?
 closure4 = { 
    print("Swift")
 }

 func Fun(myclosure: Myclosure4) { 
    myclosure()
 }

 Fun(closure4!)

Swift closure simplified wording

// 形式1: 带有参数参数类型,参数名,返回值类型
 sumClosure = { (a: Int, b: Int) -> Int in return a + b}

// 形式2: 省略参数类型
sumClosure = { (a,b) -> Int in return a + b}

// 形式3: 省略返回值类型
sumClosure = { (a,b) in return a + b}

// 形式4:省略参数小括号
sumClosure = { a,b in return a + b}

// 形式5: 省略参数
sumClosure = { return $0 + $1}

// 形式6: 省略关键字
returnsumClosure = { $0 + $1}

Swift closure in the usual manner

  1. As a non-empty variable:
var closureName: (ParameterTypes) -> ReturnType

  1. As can be empty variables:
var closureName: ((ParameterTypes) -> ReturnType)?

  1. As an alias:
typealias ClosureType = (ParameterTypes) -> ReturnType

  1. As static variables:
let closureName: ClosureType = { ... }

  1. As a parameter (defined in an alias), in function of:
funcName(parameter: (ParameterTypes) -> ReturnType)

Note: If the parameter changes the function in vitro closure, the need to add a qualifier @escaping.

  1. Callback function as a parameter:
funcName({ (ParameterTypes) -> ReturnType in statements })

  1. As a function of the parameters (no alias defined):
array.sorted(by: { (item1: Int, item2: Int) -> Bool in return item1 < item2 })

  1. As a function of the parameters (no alias defined), and hide the parameter type (for example here item1: Int):
array.sorted(by: { (item1, item2) -> Bool in return item1 < item2 })

  1. As a parameter (not alias definition), and hide return type (such as the return value of type Bool):
array.sorted(by: { (item1, item2) in return item1 < item2 })

  1. As the last parameter to the function:
array.sorted { (item1, item2) in return item1 < item2 }

  1. As the last parameter to the function, omit the parameter declarations:
array.sorted { return $0 < $1 }

  1. As the last parameter to the function, omitted and returned returnKeyword:
array.sorted { $0 < $1 }

  1. As the last parameter function returns embodied omitted, only that symbol comparison:
array.sorted(by: <)

  1. As a parameter, it includes full and clear all parameters, return value, type, and realized:
array.sorted(by: { [unowned self] (item1: Int, item2: Int) -> Bool in return item1 < item2 })

  1. As a parameter, it includes full and clear all parameters, return values, and achieved. Wherein omitted parameter types, the return type, inferences drawn by the context:
array.sorted(by: { [unowned self] in return $0 < $1 })

reference

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

https://www.jianshu.com/p/79ab32f60485

https://fuckingclosuresyntax.com/

Published 127 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103903884