An array’s elements can be removed in Swift using the remove()
method.
We only have to pass the element’s index to delete the elements.
arr.remove(at: index)
It’s return type is void
.
index
: This refers to the index position of the array to remove. In this case, the array is arr
.
// create an arrayvar vowels = ["a", "e", "p", "i", "o", "u", "k"]// print initial arrayprint(vowels)// remove elementvowels.remove(at : 2) // remove "p"//print new arrayprint(vowels)// remove elementvowels.remove(at : 5) // remove "k"// print arrayfor letter in vowels{print(letter)}
vowels
."p"
. After this, the array has six remaining elements.for-in
loop.