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

arrayName.append(element)

We can only append an element whose type is same as that of the elements in the array.

Example

In the following program, we will take an array, and append an element to this array.

main.swift

var fruits = ["apple", "banana", "cherry"]
var anotherFruit = "mango"

fruits.append(anotherFruit)

print(fruits)

Output

Swift - Append an Element to Array

The resulting array fruits gets appended with anotherFruit.

Conclusion

In this Swift Tutorial, we learned how to append an element to array.