以[a1,b1,c1]方式排列数组

时间:2017-02-15 05:31:16

标签: arrays swift

在我的应用程序中,我得到了3个名为A,B和C的数组。现在我想以这种方式排列单个数组中的所有元素[a1,b1,c1,a2,b2,c2 ]那我怎么能这样做? 请帮帮我。

1 个答案:

答案 0 :(得分:1)

let a = ["a1","a2","a3"]
let b = ["b1","b2","b3"]
let c = ["c1","c2","c3"]

var merged: [String] = []

(0..<max(a.count, b.count, c.count)).forEach {
    if $0 < a.count { merged.append(a[$0]) }
    if $0 < b.count { merged.append(b[$0]) }
    if $0 < c.count { merged.append(c[$0]) }
}
merged  // ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]