The .insert() method will add an element to a desired position of an array. When an element is added at the specified index, all later ones are pushed to the right in order to make room. This method allows for single and multiple element additions.


arrayName.insert(element, at: index)

arrayName.insert(contentsOf: [element1, element2, ...], at: index)

The method is called on an array instance at a specific index, and one or more elements can be added using the contentsOf: parameter.


var topBabyNames = ["Liam", "Olivia", "Emma", "Elijah"]
topBabyNames.insert("Noah", at: 2)
topBabyNames.insert(contentsOf: ["Oliver", "Charlotte"], at: 4)

This will output:

["Liam", "Olivia", "Noah", "Emma", "Elijah"]
["Liam", "Olivia", "Noah", "Emma", "Oliver", "Charlotte", "Elijah"]


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy