如何在Swift中串联或合并数组?

本文翻译自:How do I concatenate or merge arrays in Swift?

If there are two arrays created in swift like this: 如果有两个这样迅速创建的数组:

var a:[CGFloat] = [1, 2, 3]
var b:[CGFloat] = [4, 5, 6]

How can they be merged to [1, 2, 3, 4, 5, 6] ? 如何将它们合并到[1, 2, 3, 4, 5, 6]


#1楼

参考:https://stackoom.com/question/1hVio/如何在Swift中串联或合并数组


#2楼

You can concatenate the arrays with + , building a new array 您可以使用+连接数组,以构建新数组

let c = a + b
print(c) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

or append one array to the other with += (or append ): 或使用+=将一个数组附加到另​​一个数组(或append ):

a += b

// Or:
a.append(contentsOf: b)  // Swift 3
a.appendContentsOf(b)    // Swift 2
a.extend(b)              // Swift 1.2

print(a) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

#3楼

If you are not a big fan of operator overloading, or just more of a functional type: 如果您不是运算符重载的忠实拥护者,或者不是更多的功能类型:

// use flatMap
let result = [
    ["merge", "me"], 
    ["We", "shall", "unite"],
    ["magic"]
].flatMap { $0 }
// Output: ["merge", "me", "We", "shall", "unite", "magic"]

// ... or reduce
[[1],[2],[3]].reduce([], +)
// Output: [1, 2, 3]

#4楼

My favorite method since Swift 2.0 is flatten 自从Swift 2.0 展平以来,我最喜欢的方法

var a:[CGFloat] = [1, 2, 3]
var b:[CGFloat] = [4, 5, 6]

let c = [a, b].flatten()

This will return FlattenBidirectionalCollection so if you just want a CollectionType this will be enough and you will have lazy evaluation for free. 这将返回FlattenBidirectionalCollection因此,如果您只想要CollectionType就足够了,并且可以免费进行懒惰评估。 If you need exactly the Array you can do this: 如果您确实需要数组,可以执行以下操作:

let c = Array([a, b].flatten())

#5楼

To complete the list of possible alternatives, reduce could be used to implement the behavior of flatten : 为了完成可能的选择列表,可以使用reduce来实现flatten的行为:

var a = ["a", "b", "c"] 
var b = ["d", "e", "f"]

let res = [a, b].reduce([],combine:+)

The best alternative (performance/memory-wise) among the ones presented is simply flatten , that just wrap the original arrays lazily without creating a new array structure. 所提出的最佳选择(性能/内存方面)仅是flatten ,它只是懒散地包装原始数组,而无需创建新的数组结构。

But notice that flatten does not return a LazyCollection , so that lazy behavior will not be propagated to the next operation along the chain (map, flatMap, filter, etc...). 但是请注意, flatten 不会返回 LazyCollection ,因此,懒惰行为不会沿链(地图,flatMap,过滤器等)传播到下一个操作。

If lazyness makes sense in your particular case, just remember to prepend or append a .lazy to flatten() , for example, modifying Tomasz sample this way: 如果懒惰在您的特定情况下有意义,则只需记住将.lazy前缀或附加到flatten() ,例如,以这种方式修改Tomasz示例:

let c = [a, b].lazy.flatten()

#6楼

If you want the second array to be inserted after a particular index you can do this (as of Swift 2.2): 如果要在特定索引之后插入第二个数组,可以执行以下操作(从Swift 2.2开始):

let index = 1
if 0 ... a.count ~= index {
     a[index..<index] = b[0..<b.count]
}
print(a) // [1.0, 4.0, 5.0, 6.0, 2.0, 3.0] 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值