How do I concatenate or merge arrays in Swift?
With Swift 5, according to your needs, you may choose one of the six following ways to concatenate/merge two arrays.
#1. Merge two arrays into a new array with Array
's +(_:_:)
generic operator
Array
has a +(_:_:)
generic operator. +(_:_:)
has the following declaration:
Creates a new collection by concatenating the elements of a collection and a sequence.
static func + <Other>(lhs: Array<Element>, rhs: Other) -> Array<Element> where Other : Sequence, Self.Element == Other.Element
The following Playground sample code shows how to merge two arrays of type [Int]
into a new array using +(_:_:)
generic operator:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = array1 + array2
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#2. Append the elements of an array into an existing array with Array
's +=(_:_:)
generic operator
Array
has a +=(_:_:)
generic operator. +=(_:_:)
has the following declaration:
Appends the elements of a sequence to a range-replaceable collection.
static func += <Other>(lhs: inout Array<Element>, rhs: Other) where Other : Sequence, Self.Element == Other.Element
The following Playground sample code shows how to append the elements of an array of type [Int]
into an existing array using +=(_:_:)
generic operator:
var array1 = [1, 2, 3]
let array2 = [4, 5, 6]
array1 += array2
print(array1) // prints [1, 2, 3, 4, 5, 6]
#3. Append an array to another array with Array
's append(contentsOf:)
method
Swift Array
has an append(contentsOf:)
method. append(contentsOf:)
has the following declaration:
Adds the elements of a sequence or collection to the end of this collection.
mutating func append<S>(contentsOf newElements: S) where S : Sequence, Self.Element == S.Element
The following Playground sample code shows how to append an array to another array of type [Int]
using append(contentsOf:)
method:
var array1 = [1, 2, 3]
let array2 = [4, 5, 6]
array1.append(contentsOf: array2)
print(array1) // prints [1, 2, 3, 4, 5, 6]
#4. Merge two arrays into a new array with Sequence
's flatMap(_:)
method
Swift provides a flatMap(_:)
method for all types that conform to Sequence
protocol (including Array
). flatMap(_:)
has the following declaration:
Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.
func flatMap<SegmentOfResult>(_ transform: (Self.Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence
The following Playground sample code shows how to merge two arrays of type [Int]
into a new array using flatMap(_:)
method:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = [array1, array2].flatMap({ (element: [Int]) -> [Int] in
return element
})
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#5. Merge two arrays into a new array with Sequence
's joined()
method and Array
's init(_:)
initializer
Swift provides a joined()
method for all types that conform to Sequence
protocol (including Array
). joined()
has the following declaration:
Returns the elements of this sequence of sequences, concatenated.
func joined() -> FlattenSequence<Self>
Besides, Swift Array
has a init(_:)
initializer. init(_:)
has the following declaration:
Creates an array containing the elements of a sequence.
init<S>(_ s: S) where Element == S.Element, S : Sequence
Therefore, the following Playground sample code shows how to merge two arrays of type [Int]
into a new array using joined()
method and init(_:)
initializer:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenCollection = [array1, array2].joined() // type: FlattenBidirectionalCollection<[Array<Int>]>
let flattenArray = Array(flattenCollection)
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#6. Merge two arrays into a new array with Array
's reduce(_:_:)
method
Swift Array
has a reduce(_:_:)
method. reduce(_:_:)
has the following declaration:
Returns the result of combining the elements of the sequence using the given closure.
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
The following Playground code shows how to merge two arrays of type [Int]
into a new array using reduce(_:_:)
method:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = [array1, array2].reduce([], { (result: [Int], element: [Int]) -> [Int] in
return result + element
})
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
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]
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
):
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]