In this tutorial, you are going to learn how to remove all the elements from a Swift array with examples.
In Swift, there is a built-in method
removeAll(). We can use this method to remove all the items from our array.
Before we go forward with this tutorial, let’s consider an array:
var animals = ["Tiger", "Lion", "Zebra", "Leopard", "Bear"]
In the above array, we have 5 items. We have to remove these 5 items from the array. Below is the given program to perform this task:
var animals = ["Tiger", "Lion", "Zebra", "Leopard", "Bear"] // remove all elements from the array animals.removeAll() // Print the array after removing all elements print(animals)
As you can see, we have successfully removed all the elements from the Swift array. In the output, we can see that there are no items available to show. This is because all of them were removed using the