如何在Swift中压缩数组?

时间:2016-03-28 12:16:33

标签: arrays swift

let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

如何合并两个数组,以便输出为["Albert", "Charles", "Bobby", "David"]

2 个答案:

答案 0 :(得分:15)

您可以使用zip组合两个数组,然后将.flatMap应用于zip序列的元组元素:

let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

let arrayMerged = zip(array1,array2).flatMap{ [$0.0, $0.1] }

print(arrayMerged) // ["Albert", "Charles", "Bobby", "David"]

答案 1 :(得分:-3)

试一试

   var a = ["one", "two"]
   var b = ["three", "four"]

   var c = a + b
   print(c)