我有一组键值对:
let arr = [(key:"hey",value:["ho"]),(key:"ha",value:["tee","hee"])]
我将它分成两个数组,如下所示:
let (keys,values) = (arr.map{$0.key},arr.map{$0.value})
实际上,这与zip相反 – 我将一组元组转换为两个数组.
但我不喜欢我两次调用map的事实,因为这意味着我将循环遍历数组两次.然而,我既不想预先将两个目标数组声明为空数组,而是在追加时循环一次,例如与forEach.是否有一些很棒的Swifty成语可以将我的元组数组解压缩成两个数组?
在Swift 4中,你可以使用reduce(into :):
let (keys,values) = arr.reduce(into: ([String](),[[String]]())) { $0.0.append($1.key) $0.1.append($1.value) }
你说:
Yet neither do I want to declare the two target arrays beforehand as empty arrays and loop once while appending,e.g. with
forEach
.
就个人而言,这正是我要做的.我只想编写一个执行此操作的函数(这样您就不会使用该模式填充代码).但我认为以下内容比减少模式更加清晰和直观,但不会遭受双映射方法的低效率.
/// Unzip an `Array` of key/value tuples. /// /// - Parameter array: `Array` of key/value tuples. /// - Returns: A tuple with two arrays,an `Array` of keys and an `Array` of values. func unzip<K,V>(_ array: [(key: K,value: V)]) -> ([K],[V]) { var keys = [K]() var values = [V]() keys.reserveCapacity(array.count) values.reserveCapacity(array.count) array.forEach { key,value in keys.append(key) values.append(value) } return (keys,values) }
或者,如果您觉得有必要将其作为扩展,那么您也可以这样做:
extension Array { /// Unzip an `Array` of key/value tuples. /// /// - Returns: A tuple with two arrays,an `Array` of keys and an `Array` of values. func unzip<K,V>() -> ([K],[V]) where Element == (key: K,value: V) { var keys = [K]() var values = [V]() keys.reserveCapacity(count) values.reserveCapacity(count) forEach { key,value in keys.append(key) values.append(value) } return (keys,values) } }