To remove an element at a specific index in a Swift array, you can use the remove(at:)
method. Here's an example:
main.swift125 chars5 lines
In this example, we first create an array called numbers
with the values [1, 2, 3, 4, 5]
. Then, we declare a variable called indexToRemove
which will contain the index of the element we want to remove.
Finally, we call the remove(at:)
method on the numbers
array and pass in the indexToRemove
variable. This removes the element at the specified index and returns the removed value. Lastly, we print out the updated numbers
array which contains all elements except the one at index 2, which has been removed.
gistlibby LogSnag