如何在Swift中压缩数组? [重复]

时间:2021-09-07 21:17:00

This question already has an answer here:

这个问题在这里已有答案:

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

How do merge two array so that the out put would be ["Albert", "Charles", "Bobby", "David"]

如何合并两个数组,以便输出将是[“阿尔伯特”,“查尔斯”,“鲍比”,“大卫”]

2 个解决方案

#1


12  

You can use zip to combine your two arrays, and thereafter apply a .flatMap to the tuple elements of the zip sequence:

您可以使用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"]

#2


-3  

Give this a shot

试一试

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

   var c = a + b
   print(c)

#1


12  

You can use zip to combine your two arrays, and thereafter apply a .flatMap to the tuple elements of the zip sequence:

您可以使用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"]

#2


-3  

Give this a shot

试一试

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

   var c = a + b
   print(c)