Swift语言精要-闭包(Closure)

闭包(Closure)这个概念如果没学过Swift的人应该也不会陌生。

学过Javascript的朋友应该知道,在Javascript中我们经常会讨论闭包,很多前端工程师的面试题也会问到什么是闭包。

那么,什么是闭包呢?

让我们看下在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. )

闭包是一个函数能够访问另外一个函数作用域内变量。

(这通常通过在一个函数内申明另一个函数来实现)

通过上面的定义我们不难判断,闭包具备以下两点特征:

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 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值