What does $0 and $1 mean in Swift Closures?

后端 未结 6 2378
你的背包
你的背包 2020-12-12 12:12
let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers)

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

6条回答
  •  误落风尘
    2020-12-12 12:43

    The refer to the first and second arguments of sort. Here, sort compares 2 elements and order them. You can look up Swift official documentation for more info:

    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.

提交回复
热议问题