如何压缩两个成对的元组数组?

时间:2018-10-01 23:22:44

标签: arrays swift

我试图将其编译后抛出了很多飞镖。我有两个类似的创建的收藏集:

let ab:[(Int, Int)] = someCollection.map { thing in 
    return (a, b)
}
let cd:[(Int, Int)] = someCollection.map { thing in 
    return (c, d)
}

我想将它们结合起来,但是找不到正确的语法来给我一个有效的编译器:

let merged:[(Int, Int)] = zip(ab, cd).map { ab:(Int, Int), cd:(Int, Int) in
    return ab[1] > cd[1] ? ab : cd
}

关闭的参数列表是问题所在。我尝试了((a, b), (c, d)) in ...和许多其他变体,有无:Int提示。我总是得到:

Consecutive statements on a line must be separated by ';'

Closure tuple parameter does not support destructuring

没有办法压缩两个成对的元组列表吗?

1 个答案:

答案 0 :(得分:1)

let merged = zip(ab, cd).map { (x, y) in
    return x.1 > y.1 ? x : y
}