假设我有两个数组:
let letterArray = ["a","b","c","d","e"...] let numberArray = [1,2,3,4,5,6,7...]
我想组合两个数组,以便我得到一个输出
["a1","b2","c3","d4","e5"]
我该怎么做呢?
解决方法
您可以在地图前使用
zip(_:_:)
:
let a = ["a","e"] let b = [1,5] let result = zip(a,b).map{$0 + String($1)} print(result) // => ["a1","e5"]
zip(_:_:)
生成一个自定义Zip2Sequence
,它具有SequenceType
协议的特殊实现,因此它迭代由两个源集合组成的对.