Considering this array of tuple :
var tupleArray = [(String, Int)]()
tupleArray.append(("bonjour", 2))
tupleArray.append(("Allo", 1))
tupleArray.sort { (t1 , t2) -> Bool in
let (_, n1) = t1
let (_, n2) = t2
return n1 < n2
}
I would like to make the closure shorter by doing something like this :
tupleArray.sort { ((_, n1) , (_, n2)) -> Bool in
n1 < n2
}
First: is it possible?
Second: if possible what is the syntaxe?
Thanks
Well, you can use short closure syntax:
tupleArray.sort { $0.1 < $1.1 }
See the official guide about short closure syntax, the .1
is just tuple index access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With