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
Post a Comment