Remove Element From an Array at Specified Index in Swift

In this short Swift code example, you will learn how to:

  • Initialize an Array with 5 elements in Swift,
  • Remove an element from an array at the specified index.

Create an Array

Let’s first create a new array with some elements in it.

// Initialize Array with 5 elements
var myArray = [1,2,3,4,5]
// Print Array contents to preview
print(myArray)

Remove Element at Index

To remove an element from an array at a specified index we can use the array’s remove(at:) function.

// Remove element at index: 1
myArray.remove(at: 1)
// Print Array contents to preview
print(myArray)

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published. Required fields are marked *