更新时间:2023-11-10 07:51:57
@the_UB 和 @moonvader 都是对的,但我只是想我会稍微扩展一下@moonvader 的例子,只是为了向你展示我们是如何结束的$0 >$1
@the_UB and @moonvader are both right, but I just thought that I would extend the example from @moonvader a bit, just to show you how we end up with $0 > $1
如果您查看The Swift Programming Language"中关于 闭包表达式 你可以看到,为了对数组进行排序,你调用了 sort
方法,然后该方法可以将一个函数作为参数.
If you look at the example in "The Swift Programming Language" about Closure Expressions you can see that to sort an array you call the sort
method which can then take a function as a parameter.
这个函数必须接受两个参数并比较它们,然后返回一个布尔值.
This function must take two parameters and compare them, and then return a boolean.
所以如果我们有这个数组:
So if we have this array:
让数字 = [4, 6, 8, 1, 3]
还有这个方法
func sortBackwards(val1: Int, val2: Int) -> Bool {
print("val1: \(val1) - val2: \(val2)" )
return val1 > val2
}
我们可以像这样对元素进行排序:
We can sort the elements like so:
numbers.sort(sortBackwards)//给我们 [8, 6, 4, 3, 1]
sort
方法将对数组中的每个元素使用我们的 sortBackwards
方法并比较它们.
The sort
method will use our sortBackwards
method on each of the elements in the array and compare them.
这是print
val1: 6 - val2: 4
val1: 8 - val2: 4
val1: 8 - val2: 6
val1: 1 - val2: 4
val1: 3 - val2: 1
val1: 3 - val2: 4
好的,让我们减少它.
我们可以将其作为参数直接添加到 sort
方法中,而不是定义一个函数,如下所示:
Instead of defining a function, we can add that directly as a parameter to the sort
method like so:
numbers.sort({(val1: Int, val2: Int) -> Bool in
return val1 > val2
})
我们还是得到了 [8, 6, 4, 3, 1](太幸运了!)
And we still end up with [8, 6, 4, 3, 1] (how fortunate!)
好的,接下来我们可以做的是The Swift Programming Language"(上面的链接)中称为从上下文推断类型"的内容.当我们在 Int
数组上调用这个方法时,Swift 可以确定我们的 val1
和 val2
参数必须是 Int
s 也是,我们不需要告诉它.所以,让我们删除类型.这给我们留下了:
OK, the next thing we can do is what in "The Swift Programming Language" (the link above) is called "Infering Type From Context". As we call this method on an array of Int
s, Swift can figure out that our val1
and val2
parameters must be Int
s too, there's no need for us to tell it. So, lets remove the types. That leaves us with:
numbers.sort({val1, val2 in
return val1 > val2
})
结果还是一样.
好的,到了.接下来我们可以做的是书中所说的单表达式闭包的隐式返回"
OK, getting there. The next thing we can do is what in the book is called "Implicit Returns from Single-Expression Closures"
因为我们的比较可以在一行中完成,所以我们不需要使用 return
.所以:
As our comparison can be done in one line there's no need for us to use return
. So:
numbers.sort({val1, val2 in val1 > val2})
仍然给我们 [8, 6, 4, 3, 1]
Still gives us [8, 6, 4, 3, 1]
最后我们得到了@moonvader 用更少的词来解释的内容:-) 即简写参数名称"
Finally we're getting to what @moonvader used much much less words to explain :-) namely "Shorthand Argument Names"
正如书中所说:
Swift 自动为内联闭包提供速记参数名称,可用于通过名称 $0、$1、$2 等引用闭包参数的值.
Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.
因此,在我们的示例中,val1
可以替换为 $0
,而 val2
可以替换为 $1
So, in our example, val1
can be replaced by $0
and val2
can be replaced by $1
这给了我们:
numbers.sort({$0 > $1})
我们仍然得到 [8, 6, 4, 3, 1]
And still we get [8, 6, 4, 3, 1]
然后我们可以继续使用Trailing Closure",这意味着如果函数的最后一个参数是一个闭包,我们可以在函数的外部"添加该参数.
We can then continue to use a "Trailing Closure", which means that if the last parameter of a function is a closure, we can add that parameter "outside" the function.
所以我们最终得到:
numbers.sort{$0 > $1}
结果还是 [8, 6, 4, 3, 1]
And the outcome is still [8, 6, 4, 3, 1]
希望有助于澄清事情.