Swift:连接具有不同索引数量的数组

时间:2019-12-18 18:17:01

标签: arrays swift for-loop

我有2个与zip组合的数组,我希望它们交替(A1索引0、2、3 ...),没有问题。 当数组大小不同时,for循环仅在短数组的持续时间内运行,而忽略了一些较长的数组。有什么提示吗?我尝试了不同的循环,大步前进,但没有喜悦。 即使在更长的数组中,如何获取for循环遍历所有索引?

var a1 = ["one", "three", "five"]
var a2 = ["two", "four"]
var joined = [String]()

for (odd, even) in zip(a1, a2) {
    joined.append(odd)
    joined.append(even)
    }

print(joined) // ["one", "two", "three", "four"] <- the "five" is missing

0 个答案:

没有答案