闭包 Closure
闭包的基本使用与简写
- 语法优化
var myClosure:() -> Void = { print("Hello world") } myClosure() var mySecondClosure:(Int,Int) -> Int = { (a:Int,b:Int) -> Int in //闭包标准写法 return a*b } mySecondClosure = { (a,b) in //闭包简写,类型自动推导 可以根据参数推断 return a*b } mySecondClosure = { (a,b) in //进一步闭包简写,如果函数体只包含一句 return 代码,可省略 return a*b } mySecondClosure = { $0 * $1 //再简写:被捕获的参数列表中,含有a、b,下标从0开始,可通过"$"获取。省略参数列表 (a, b)和 关键字 in }
闭包应用
- 排序
var arr:[Int] = [1,3,5,6,7,2,4,6,8] arr.sorted() //[1, 2, 3, 4, 5, 6, 6, 7, 8] arr.sorted { $0 > $1 //[8, 7, 6, 6, 5, 4, 3, 2, 1] }
var arr = [0,1,2,3,4,5,6,7,8,9,10] arr.sorted { return fabs(Float($0-5)) < fabs(Float($1-5)) //离5越近排在越前面 [5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10] }
- 遍历
//forEach var arr2 = [5,6,7,8,9] arr2.forEach { print("\($0)") //[5, 6, 7, 8, 9] } var arr3 = [1,2,3,4] //filter arr3.filter { return $0 > 3 //[4] } var arr4 = ["0","1","imagine"] //map var num = arr4.map { Int($0) //[0, 1, nil] 类型转换可能失败,所以返回的是可选型 } var num2 = arr4.compactMap { Int($0) //[0, 1] //compactMap : 解包->展开并合并 } //reduce var arr5 = [0,1,2] var sum = arr5.reduce(0) { return $0 + $1 //3 }
闭包作为函数参数
func handler(_ a: Int, _ b: Int,operation:(Int,Int)->Int) ->Int { let res = operation(a,b) return res } let multipyClosure = { //实现一个闭包 (a:Int,b:Int) in a * b } handler(2, 3, operation: multipyClosure) //将闭包作为参数传递
捕获
闭包可以从上下文环境中捕获常量、变量,并在自己的作用域内使用。
//捕获 var num = 3 arr.sorted { return fabs(Float($0-num)) < fabs(Float($1-num)) //向外层找变量 num }