Replaces the specified subrange of elements with the given collection.
mutating func replaceSubrange<C, R>(_ subrange: R, with newElements: C) where C : Collection, R : RangeExpression, Self.Element == C.Element, Self.Index == R.Bound
The subrange of the collection to replace. The bounds of the range must be valid indices of the collection.
The new elements to add to the collection.
This method has the effect of removing the specified range of elements from the collection and inserting the new elements at the same location. The number of new elements need not match the number of elements being removed.
In this example, three elements in the middle of an array of integers are replaced by the five elements of a
var nums = [10, 20, 30, 40, 50] nums.replaceSubrange(1...3, with: repeatElement(1, count: 5)) print(nums) // Prints "[10, 1, 1, 1, 1, 1, 50]"
If you pass a zero-length range as the
subrange parameter, this method inserts the elements of
subrange.startIndex. Calling the
insert(contentsOf:at:) method instead is preferred.
Likewise, if you pass a zero-length collection as the
newElements parameter, this method removes the elements in the given subrange without replacement. Calling the
removeSubrange(_:) method instead is preferred.
Calling this method may invalidate any existing indices for use with this collection.