Swift Array – Remove All Elements

To remove all elements of an Array in Swift, call removeAll() method on the array.

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

arrayName.removeAll(where: condition)

where parameter is optional, and hence if we do not provide an condition, all the elements in the array are deleted.

Example

In the following program, we will take an array fruits with five elements, and remove all of its elements using removeAll() method.

main.swift

var fruits = ["apple", "banana", "cherry", "mango", "guava"]
fruits.removeAll()
print(fruits)

Output

Swift Array - Remove All Elements

All the elements have been removed from the array, and an empty array is printed in the output.

ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to remove all the elements from an array in Swift.