Swift – Append an Element to Array
To append an element to array in Swift, call
append(_:) method on this array and pass the element as argument to the method.
append(_:) method appends the given element to the end of this array.
The syntax to append an element to the array is
We can only append an element whose type is same as that of the elements in the array.
In the following program, we will take an array, and append an element to this array.
var fruits = ["apple", "banana", "cherry"] var anotherFruit = "mango" fruits.append(anotherFruit) print(fruits)
The resulting array
fruits gets appended with
In this Swift Tutorial, we learned how to append an element to array.