http://www.hangge.com/blog/cache/detail_1829.html
本文主要来讲讲 Swift 中提供的 zip 这个函数。这个 zip 函数可不是用来压缩文件的,其作用是将两个序列的元素,一一对应合并生成一个新序列。
该函数一般不会单独使用,而是会和其它的方法配合使用。下面我通过几个样例来演示它的用法。
1,将两个数组合并成一个新的元组数组
(1)下面代码我们使用 zip 结合 map 来合并生成新数组。注意:zip 函数生成的新序列个数为原始序列的最小值。
1 2 3 4 | let a = [1, 2, 3, 4, 5] let b = [ "a" , "b" , "c" , "d" ] let c = zip(a, b). map { $0 } print (c) |

(2)由于 zip 过程中较短的一个序列结束后整个操作就会停止,我们这里还可以使用单向区间。下面代码的运行结果同上面是一样的。
1 2 3 | let b = [ "a" , "b" , "c" , "d" ] let c = zip(1..., b). map { $0 } print (c) |
2,通过键值序列创建字典
(1)下面将两个数组合并成一个字典。
1 2 3 4 | let names = [ "Apple" , "Pear" ] let prices = [7, 6] let dict = Dictionary (uniqueKeysWithValues: zip(names, prices)) print (dict) |

(2)zip 配合速记 + 可以用来解决重复键的问题。比如下面将数组转为字典,字典键为数组元素值,字典值为该元素出现的次数。
1 2 3 | let array = [ "Apple" , "Pear" , "Pear" , "Orange" ] let dic = Dictionary (zip(array, repeatElement(1, count: array.count)), uniquingKeysWith: +) print (dic) |

3,将两个数组合并成一个新数组
我们知道 flatMap 函数还能把数组中存有数组的数组(二维数组、N维数组)一同打开变成一个新的数组,不过新数组里元素的顺序是根据原数组顺序一个接着一个的。
而配合 zip 可以让两个数组元素间隔地插入。下面代码分别比较这两种方式。
1 2 3 4 5 6 7 8 | let a = [ "a" , "b" , "c" , "d" ] let b = [ "A" , "B" , "C" , "D" ] let c = [a, b].flatMap({ $0 }) print ( "c:\(c)" ) let d = zip(a, b).flatMap({ [$0, $1] }) print ( "d:\(d)" ) |

4,根据 String 数组生成对应的按钮数组
1 2 3 4 5 6 7 8 | let titles = [ "按钮1" , "按钮2" , "按钮3" ] let buttons = zip(0..., titles). map { (i, title) -> UIButton in let button = UIButton (type: .system) button.setTitle(title, for :.normal) button.tag = i return button } |
5,将按钮数组里的按钮设置成对应颜色数组里的颜色
1 2 3 | zip( self .buttons, self .colors).forEach { (button, color) in button.backgroundColor = color } |
上面还可以简写成如下:
1 2 3 | zip( self .buttons, self .colors).forEach { $0.0.backgroundColor = $0.1 } |
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1829.html