2017-02-15 76 views
-5

在我的應用程序我得到3個陣列則名爲分別爲A,B和C.現在我想安排在單個陣列中的所有元素以這種方式[A1,B1, c1,a2,b2,c2]我該怎麼做? 請幫幫我。安排[A1,B1,C1]的方式排列

+3

顯示你試過代碼 –

+0

使用Zip3Sequence從我對這個問題的答案:http://stackoverflow.com/questions/40517760/is-there-a-zip-function-to-create-tuples-with-多於2個元素 – Alexander

回答

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"] 
+1

執行已知數量的附加操作時,應始終保留容量。 – Alexander