Swift Closures 中的 $0 和 $1 是什么意思? [英] What does $0 and $1 mean in Swift Closures?

查看:35
本文介绍了Swift Closures 中的 $0 和 $1 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers)

谁能解释一下,$0$1 在 swift 中是什么意思?

Can anyone explain, what $0 and $1 means in swift?

更多示例

array.forEach {
    actions.append($0)
}

推荐答案

$0 是传递给闭包的第一个参数.$1 是第二个参数,等等.你展示的那个闭包是以下的简写:

$0 is the first parameter passed into the closure. $1 is the second parameter, etc. That closure you showed is shorthand for:

let sortedNumbers = numbers.sort { (firstObject, secondObject) in 
    return firstObject > secondObject
}

这篇关于Swift Closures 中的 $0 和 $1 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆