Swift Array – Remove Last Element

To remove last element from Array in Swift, call removeLast() method on this array.

removeLast() method removes the last element from this array.

The syntax to call removeLast() method on the array is

</>
Copy
arrayName.removeLast()

Examples

In the following program, we will take an array fruits, and remove the last element using removeLast() method.

main.swift

</>
Copy
var fruits = ["apple", "banana", "cherry", "mango"]
fruits.removeLast()
print(fruits)

Output

Swift Array - Remove Last Element

The last element "mango" has been removed from the array fruits.

If the array is empty, removing last element using removeLast() causes a runtime error as shown in the following example.

main.swift

</>
Copy
var fruits: [String] = []
fruits.removeLast()
print(fruits)

Output

Conclusion

In this Swift Tutorial, we learned how to remove last element from array in Swift.