Swift - Removing Elements from an Array

Introduction

You can remove elements from an array using the following functions:

var OSes :[String]  = ["iOS", "Android", "Windows"]
var os1 = OSes. removeAtIndex (3)    
var os2 = OSes. removeLast ()        
OSes.removeAll (keepCapacity: true) 

Both the removeAtIndex() and removeLast() functions return the item removed.

For the removeAll() function, it clears all elements in the array.

If the keepCapacity parameter is set to true , then the array will maintain its size.

Keeping the capacity means that additional elements can be stored later without needing the array to trigger a reallocation of the backing storage.

Related Topic