Swift – Append/Concatenate Arrays
Welcome to Swift Tutorial. In this tutorial, we will learn how to append two arrays with the help of examples.
To append or concatenate two arrays in Swift, use plus +
operator with the two arrays as operands.
Following is a quick example to append an array at the end of another array.
</>
Copy
var new_array = array_1 + array_2
You have to remember that the elements of array_2 are appended to that of array_1 in the new array.
Example 1 – Append two Integer Arrays in Swift
In this example Swift program, we will append or concatenate two integer arrays.
main.swift
</>
Copy
var array1:[Int] = [22, 54]
var array2:[Int] = [35, 68]
var new_array = array1 + array2
for str in new_array {
print( "\(str)" )
}
Output
22
54
35
68
Example 2 – Append two String Arrays in Swift
In this example program, we will append or concatenate two integer arrays.
main.swift
</>
Copy
var array1:[String] = ["TutorialKart","Swift Tutorial"]
var array2:[String] = ["iOS Tutorial","Best Tutorials"]
var new_array = array1 + array2
for str in new_array {
print( "\(str)" )
}
Output
TutorialKart
Swift Tutorial
iOS Tutorial
Best Tutorials
Conclusion
In this Swift Tutorial, we have learned to append or concatenate two arrays in Swift programming.