闭包(Closure)这个概念若是没学过Swift的人应该也不会陌生。前端
学过Javascript的朋友应该知道,在Javascript中咱们常常会讨论闭包,不少前端工程师的面试题也会问到什么是闭包。面试
那么,什么是闭包呢?swift
让咱们看下在Javascript中闭包的解释:数组
Closures are functions that have access to variables from another function’s scope. 前端工程师
(This is often accomplished by creating a function inside a function. )闭包
闭包是一个函数可以访问另一个函数做用域内变量。app
(这一般经过在一个函数内申明另外一个函数来实现)ide
经过上面的定义咱们不难判断,闭包具有如下两点特征:函数
1. 闭包是函数测试
2. 闭包是函数内部的函数
3. 闭包能够访问另外一个函数的变量
咱们先来看下Javascipt闭包的例子:
function createComparisonFunction(propertyName) { return function(object1, object2) { var value1 = object1[propertyName]; var value2 = object2[propertyName]; if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } }; }
测试代码:
var compare = createComparisonFunction(“name”); var result = compare({ name: “Nicholas”, age: 35 }, { name: “Greg”, age: 28 }); console.log(result); // 1
上面的Javascript代码中咱们能够看到,
createComparisonFunction函数内部申明的匿名函数能够访问createComparisonFunction函数自己的参数,这正符合了咱们闭包的定义。
好了,Swift中闭包大体也是相似的,咱们来看下闭包在Swift中的定义:
Closures are discrete bundles of functionality that can be used in your application to accomplish specific tasks.
闭包是用来在你的应用程序内部完成某些特殊任务的一些彼此互不相关的函数集。
读着有些拗口,仍是让咱们用例子来讲话吧,看下下面的例子:
咱们先来看一个不适用闭包的数组排序的例子
import Cocoa // 不使用闭包 var counts = [1, 6, 2, 28, 13, 2, 9, 100, 30] func sortAscending(i: Int, j: Int) -> Bool { return i < j } let sortedCounts = counts.sort(sortAscending)
如今,咱们看下,使用闭包代码会变得更加简洁
import Cocoa // 使用闭包 var counts = [1, 6, 2, 28, 13, 2, 9, 100, 30] let sortedCounts = counts.sort({ (i: Int, j: Int) -> Bool in return i < j })
Swift的闭包语法:
{(parameters) -> [return type] in
// Code
}
经过类型推断(type inference)让闭包变得更加简洁:
import Cocoa var counts = [1, 6, 2, 28, 13, 2, 9, 100, 30] let sortedCounts = counts.sort({ i, j in i < j })
经过参数占位符让代码变得更简洁:
import Cocoa var counts = [1, 6, 2, 28, 13, 2, 9, 100, 30] let sortedCounts = counts.sort({ $0 < $1 })
不难判断,上面的代码$0表明了第一个参数也就是i, 而$1表明了第一个参数也就是j。
哇撒,代码变得愈来愈短了嘛,还能够再短些吗?
答案是能够的!
还记得C++的内联函(inline function)数嘛,在swift中咱们也有!
把括号去掉就是了!
因而,代码变成了以下的样子:
import Cocoa var counts = [1, 6, 2, 28, 13, 2, 9, 100, 30] let sortedCounts = counts.sort { $0 < $1 }