Unpack nested tuple in Swift closure -


if have:

let array = [(1, 2)] 

you can do:

array.map({ first, second in ... }) 

what if have:

let array = [((1, 2), (3, 4))] 

how can unpacked?

array.map({ (first, second), (third, fourth) in ... }) array.map({ ((first, second), (third, fourth)) in ... }) 

neither of these compile.

this works but's it's not pretty

let array = [((1, 2), (3, 4))]  array.map({ a, b in     return a.0 + a.1 + b.0 + b.1 }) 

this might little bit better it's still nested

let array = [((1, 2), (3, 4))]  array.map({ (a:(first:int, second:int), b:(first:int, second:int)) in     return a.first + a.second + b.first + b.second }) 

you use map reduce them tuple , assign values

array.map{($0.0,$0.1,$1.0,$1.1)}.map { (first:int, second:int, third:int, forth:int) in } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -