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
</>
Copy
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
</>
Copy
var fruits = ["apple", "banana", "cherry", "mango", "guava"]
fruits.removeAll()
print(fruits)
Output
All the elements have been removed from the array, and an empty array is printed in the output.
Conclusion
In this Swift Tutorial, we learned how to remove all the elements from an array in Swift.