[英]Why sort([1,2,3]){true} doesn't work in Swift?
我知道这个例子是完全没有用的和人为的,但是我试图更好地围绕Swift中的值处理方式,我不明白为什么下面的代码会返回错误。
var notSorted = sort([1,2,3,4]){true}
The second parameter of sort
has type (T, T) -> Bool
, but you are supplying a () -> Bool
, namely a closure with no parameters that returns true
. sort
的第二个参数的类型为(T, T) -> Bool
,但是您提供的是() -> Bool
,即没有参数的闭包返回true
。
Of course the two types don't match and you rightfully get a compile-time error. 当然,这两种类型不匹配,您理应得到编译时错误。
Something like this will work: 这样的事情会起作用:
var notSorted = sort([1,2,3,4]){_ in true}
By the way, that closure has the effect of reversing the array. 顺便说一下,该关闭具有反转数组的作用。 If you want to perform a nop, you have to put
false
. 如果要执行点动操作,则必须输入
false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.