Highlight Tuple Members in Closing Arguments

Given this array of tuples:

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 this possible? Second: if possible, what is the syntax?

thanks

+6
source share
1 answer

Well, you can use a short closing syntax:

 tupleArray.sort { $0.1 < $1.1 } 

See the official guide on short circuit syntax, .1 is just index access to a tuple.

+3
source

Source: https://habr.com/ru/post/985946/


All Articles