Swift Array append()

The append() method adds a new element at the end of the array.

Example


append() Syntax

The syntax of the array append() method is:

array.append(newElement)

Here, array is an object of the Array class.


append() Parameters

The append() method takes a single parameter:

  • newElement - element to be added to array

append() Return Value

The append() method doesn't return any value. It only updates the current array.


Example 1: Swift Array append()

Output

["Swift", "C", "Java", "C++"]
[12, 21, 35, 44]

Example 2: Adding One Array To Another Array

Output

["cat", "dog", "tiger", "fox"]

In the above example, we have used the append() method and contentsOf property to append the elements of wildAnmals to animals.