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]
?
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]
?
Marge array that are different data types :
Output :
With Swift 3, according to your needs and tastes, you may choose one of the five following ways to concatenate/merge two arrays.
1. Merge two arrays into a new array with
Swift standard library
's+(_:_:)
generic operatorSwift standard library defines a
+(_:_:)
generic operator.+(_:_:)
has the following declaration:The following Playground code shows how to merge two arrays of type
[Int]
into a new array using+(_:_:)
generic operator:2. Append an array to another array with
Array
'sappend(contentsOf:)
methodSwift
Array
has anappend(contentsOf:)
method.append(contentsOf:)
has the following declaration:The following Playground code shows how to append an array to another array of type
[Int]
usingappend(contentsOf:)
method:3. Merge two arrays into a new array with
Sequence
'sflatMap(_:)
methodSwift provides a
flatMap(_:)
method for all types that conform toSequence
protocol (includingArray
).flatMap(_:)
has the following declaration:The following Playground code shows how to merge two arrays of type
[Int]
into a new array usingflatMap(_:)
method:4. Merge two arrays into a new array with
Sequence
'sjoined()
method andArray
'sinit(_:)
initializerSwift provides a
joined()
method for all types that conform toSequence
protocol (includingArray
).joined()
has the following declaration:Besides, Swift
Array
has ainit(_:)
initializer.init(_:)
has the following declaration:Therefore, the following Playground code shows how to merge two arrays of type
[Int]
into a new array usingjoined()
method andinit(_:)
initializer:5. Merge two arrays into a new array with
Array
'sreduce(_:_:)
methodSwift
Array
has areduce(_:_:)
method.reduce(_:_:)
has the following declaration:The following Playground code shows how to merge two arrays of type
[Int]
into a new array usingreduce(_:_:)
method:Here's the shortest way to merge two arrays.
Concatenate/merge them
To complete the list of possible alternatives,
reduce
could be used to implement the behavior of flatten: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.But notice that flatten does not return a
LazyColletion
, so that lazy behavior will not be propagated to the next operation along the chain (map, flatMap, filter, etc...).If lazyness makes sense in your particular case, just remember to prepend or append a
.lazy
toflatten()
, for example, modifying Tomasz sample this way:if you want result as : [1,2,3,[4,5,6]]
above code will convert arrayOne as a single element and add it to the end of arrayTwo.
if you want result as : [1, 2, 3, 4, 5, 6] then,
above code will add all the elements of arrayOne at the end of arrayTwo.
Thanks.
If you want the second array to be inserted after a particular index you can do this (as of Swift 2.2):