• 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 }
  • 相关阅读:
    winform 监视DataGridView的滚动条,加载数据
    SQL 自动生成行号
    SQL 删除重复的数据(多个字段判断),只保留一条数据
    C# 一个简单的 工厂模式 例子
    winform 重写TextBox的OnTextChanged方法(事件)
    winform 给文本框加载内容的时候 始终让文本框的滚动条到底(允许显示多行Multiline=True)
    winform 制定DataGridViewTextColumn列(更改DataGridView的Cell的状态很有用)
    winform 使用委托 实现多线程访问控件
    C# 一个简单的递归函数和 两个List<T> 合并
    ADO.NET  把数据库A的某表的数据内容复制到数据库B的某表内
  • 原文地址:https://www.cnblogs.com/davidgu/p/6065474.html
Copyright © 2020-2023  润新知