insert(_:at:)
Inserts a new element at the specified position.
Declaration
mutating func insert(_ newElement: Element, at i: Int)Parameters
- newElement:
The new element to insert into the array.
- i:
The position at which to insert the new element.
indexmust be a valid index of the array or equal to itsendIndexproperty.
Discussion
The new element is inserted before the element currently at the specified index. If you pass the array’s endIndex property as the index parameter, the new element is appended to the array.
var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)
print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"