简体   繁体   English

这种类型的封闭意味着什么,它如何工作?

[英]What's this type of Closure supposed to mean and how does it work?

Got it from Here 这里得到

The Ultimate Closure Finally, for the ultra parsimonious there is the following, without a byte wasted. 最终的关闭最后,对于超简约的应用,有以下内容,而不会浪费字节。

let testEquality9 : (Int, Int) -> Bool = (==)

This is not actually a closure, it's the equality operator that compares two integers stored into a variable. 这实际上不是闭包,而是相等运算符,用于比较存储在变量中的两个整数。

Every operator is defined using a function and that function can be assigned to a variable. 每个运算符都使用一个函数定义,并且可以将该函数分配给变量。 There is nothing else to it. 没有别的了。

Functions decalred with the func keyword are just closures with names. func关键字修饰的func只是带有名称的闭包。 == is an example of one such named function. ==是一个这样的命名函数的示例。 It takes 2 Int arguements, and returns a Bool telling you if they're equal. 它需要2个Int争论,然后返回Bool告诉您它们是否相等。 Its type is (Int, Int) -> Bool 它的类型是(Int, Int) -> Bool

testEquality9 is a closure, with the type (Int, Int) -> Bool . testEquality9是一个闭包,类型为(Int, Int) -> Bool To it, the closure of the == function is assigned. 为此,分配了==函数的闭包。

It can be called like this: 可以这样称呼:

testEquality9(1, 2) // false
testEquality9(1, 1) // true

The key thing to draw from this is that functions are really just closures, so they can be used everywhere closures can be used. 从中得出的关键是,函数实际上只是闭包,因此可以在可以使用闭包的任何地方使用它们。

For example, if you wanted to sort an array of Ints, you could use: 例如,如果您想对一个整数数组进行排序,则可以使用:

let ints = [3, 1, 4, 2]
let sorted = ints.sort{$0 < $1}

The sort(_:) method takes a closure that's of type (Int, Int) -> Bool . sort(_:)方法采用类型(Int, Int) -> Bool的闭包。 Our closure {$0 < $1} takes 2 Int params, and returns a Bool . 我们的闭包{$ 0 <$ 1}接受2个Int参数,并返回一个Bool So it fits that signiture. 因此,它符合该签名。

However, we can make this code shorter. 但是,我们可以使这段代码更短。 Because the < operator's function already has type (Int, Int) -> Bool , we can write this: 因为<运算符的功能已经具有类型(Int, Int) -> Bool ,所以我们可以这样写:

let sorted = ints.sort(<)

This passes the function (named closure) < in directly, without explicitly making our own closure to wrap around it. 这将直接传递函数(称为闭包) < ,而无需显式地创建我们自己的闭包即可。

Operator Overloading: 运算符重载:

func == (i : Int, j: Int) -> Bool {
     return i == j
}

Should be equivalent to that. 应该等效。

As the others said, it's the abbreviation form of: 正如其他人所说,它是以下内容的缩写形式:

let testEquality9: (Int, Int) -> Bool = { (a: Int, b: Int) -> Bool in return a == b }

Reading from right to left, it creates a function that compares two Ints and assigns it to the constant testEquality9. 从右向左读取,它创建一个比较两个Ints并将其分配给常量testEquality9的函数。

You need to mentally separate the 3 pieces: 您需要在精神上将这三个部分分开:

The constant name: 常量名称:

let testEquality9

The constant type (it's a function type): 常量类型(它是一个函数类型):

(Int, Int) -> Bool

And the value assigned to the constant: 并将值赋给常量:

(==)

OR, the long version: 或者,长版:

{ (a: Int, b: Int) -> Bool in return a == b }

Enjoy Swift :) 享受Swift :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM