Protocol

RangeReplaceableCollection

A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.

Overview

Range-replaceable collections provide operations that insert and remove elements. For example, you can add elements to an array of strings by calling any of the inserting or appending operations that the RangeReplaceableCollection protocol defines.

var bugs = ["Aphid", "Damselfly"]
bugs.append("Earwig")
bugs.insert(contentsOf: ["Bumblebee", "Cicada"], at: 1)
print(bugs)
// Prints "["Aphid", "Bumblebee", "Cicada", "Damselfly", "Earwig"]"

Likewise, RangeReplaceableCollection types can remove one or more elements using a single operation.

bugs.removeLast()
bugs.removeSubrange(1...2)
print(bugs)
// Prints "["Aphid", "Damselfly"]"

bugs.removeAll()
print(bugs)
// Prints "[]"

Lastly, use the eponymous replaceSubrange(_:with:) method to replace a subrange of elements with the contents of another collection. Here, three elements in the middle of an array of integers are replaced by the five elements of a Repeated<Int> instance.

 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]"

Conforming to the RangeReplaceableCollection Protocol

To add RangeReplaceableCollection conformance to your custom collection, add an empty initializer and the replaceSubrange(_:with:) method to your custom type. RangeReplaceableCollection provides default implementations of all its other methods using this initializer and method. For example, the removeSubrange method is implemented by calling replaceRange with an empty collection for the newElements parameter. You can override any of the protocol’s required methods to provide your own custom implementation.

Protocol Requirements

Associated Types

Initializers

init()

Creates a new, empty collection.

init<S>(S)

Creates a new instance of a collection containing the elements of a sequence.

init(repeating: Self.Iterator.Element, count: Int)

Creates a new collection containing the specified number of a single, repeated value.

Instance Methods

func append(Self.Iterator.Element)

Adds an element to the end of the collection.

func append<S>(contentsOf: S)
func insert(Self.Iterator.Element, at: Self.Index)

Inserts a new element into the collection at the specified position.

func insert<S>(contentsOf: S, at: Self.Index)

Inserts the elements of a sequence into the collection at the specified position.

func remove(at: Self.Index)

Removes and returns the element at the specified position.

func removeAll(keepingCapacity: Bool)

Removes all elements from the collection.

func removeFirst()

Removes and returns the first element of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func replaceSubrange<C>(Range<Self.Index>, with: C)

Replaces the specified subrange of elements with the given collection.

func reserveCapacity(Self.IndexDistance)

Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.

Default Implementations

Initializers

init<S>(S)

Creates a new instance of a collection containing the elements of a sequence.

init(repeating: Self.Iterator.Element, count: Int)

Creates a new collection containing the specified number of a single, repeated value.

Instance Methods

func append(Self.Iterator.Element)

Adds an element to the end of the collection.

func append<S>(contentsOf: S)

Adds the elements of a sequence or collection to the end of this collection.

func insert(Self.Iterator.Element, at: Self.Index)

Inserts a new element into the collection at the specified position.

func insert<C>(contentsOf: C, at: Self.Index)

Inserts the elements of a sequence into the collection at the specified position.

func remove(at: Self.Index)

Removes and returns the element at the specified position.

func removeAll(keepingCapacity: Bool)

Removes all elements from the collection.

func removeFirst()

Removes and returns the first element of the collection.

func removeFirst()

Removes and returns the first element of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func removeLast()

Removes and returns the last element of the collection.

func removeLast()

Removes and returns the last element of the collection.

func removeLast(Int)

Removes the specified number of elements from the end of the collection.

func removeLast(Int)

Removes the specified number of elements from the end of the collection.

func removeSubrange(ClosedRange<Self.Index>)

Removes the elements in the specified subrange from the collection.

func removeSubrange(Range<Self.Index>)

Removes the elements in the specified subrange from the collection.

func removeSubrange(CountableClosedRange<Self.Index>)

Removes the elements in the specified subrange from the collection.

func removeSubrange(CountableRange<Self.Index>)

Removes the elements in the specified subrange from the collection.

func replaceSubrange<C>(CountableClosedRange<Self.Index>, with: C)

Replaces the specified subrange of elements with the given collection.

func replaceSubrange<C>(ClosedRange<Self.Index>, with: C)

Replaces the specified subrange of elements with the given collection.

func replaceSubrange<C>(CountableRange<Self.Index>, with: C)

Replaces the specified subrange of elements with the given collection.

func reserveCapacity(Self.IndexDistance)

Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.