Aashish Tamsya Aashish Tamsya - 13 days ago 11
iOS Question

What does $0 and $1 mean in swift 2.1?

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


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

Answer

$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
}