RangeReplaceableCollection
A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.
protocol RangeReplaceableCollection<Element> : Collection where Self.SubSequence : RangeReplaceableCollection
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 replaceSubrange(_:with:)
with an empty collection for the newElements
parameter. You can override any of the protocol’s required methods to provide your own custom implementation.
Supertypes
protocol Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol Sequence
A type that provides sequential, iterated access to its elements.
Requirements
associatedtype SubSequence
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.Element, count: Int
) Creates a new collection containing the specified number of a single, repeated value.
subscript(Range
<Self.Index>) -> Self.SubSequence subscript(Self
.Index) -> Self.Element func append(Self
.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
.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
) -> Self.Element Removes and returns the element at the specified position.
func removeAll(keepingCapacity: Bool
) Removes all elements from the collection.
func removeAll(where: (Self.Element) throws -> Bool
) rethrows Removes all the elements that satisfy the given predicate.
func removeFirst(
) -> Self.Element 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 removeSubrange(Range
<Self.Index>) Removes the specified subrange of elements from the collection.
func replaceSubrange<C>(Range
<Self.Index>, with: C) Replaces the specified subrange of elements with the given collection.
func reserveCapacity(Int
) Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.
Citizens in Swift
Members
static func + <Other>(Other, Self
) -> Self Creates a new collection by concatenating the elements of a sequence and a collection.
static func + <Other>(Self, Other
) -> Self Creates a new collection by concatenating the elements of a collection and a sequence.
static func + <Other>(Self, Other
) -> Self Creates a new collection by concatenating the elements of two collections.
static func += <Other>(inout Self, Other
) Appends the elements of a sequence to a range-replaceable collection.
func applying(CollectionDifference
<Self.Element>) -> Self? Applies the given difference to this collection.
func filter((Self.Element) throws -> Bool
) rethrows -> Self Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.
Citizens in Swift
where Self:BidirectionalCollection
Members
func popLast(
) -> Self.Element? Removes and returns the last element of the collection.
func removeLast(
) -> Self.Element Removes and returns the last element of the collection.
func removeLast(Int
) Removes the specified number of elements from the end of the collection.
Citizens in Swift
where Self:BidirectionalCollection, Self == Self.SubSequence
Members
func popLast(
) -> Self.Element? Removes and returns the last element of the collection.
func removeLast(
) -> Self.Element Removes and returns the last element of the collection.
func removeLast(Int
) Removes the specified number of elements from the end of the collection.
Available in _StringProcessing
Members
Available in _StringProcessing
where Self:BidirectionalCollection, Self.SubSequence == Substring
Members
func trimPrefix(some RegexComponent
) Removes the initial elements that matches the given regex.
Available in _StringProcessing
where Self.Element:Equatable
Members
func replace<C, Replacement>(C, with: Replacement, maxReplacements: Int
) Replaces all occurrences of a target sequence with a given collection
func replacing<C, Replacement>(C, with: Replacement, maxReplacements: Int
) -> Self Returns a new collection in which all occurrences of a target sequence are replaced by another collection.
func replacing<C, Replacement>(C, with: Replacement, subrange: Range<Self.Index>, maxReplacements: Int
) -> Self Returns a new collection in which all occurrences of a target sequence are replaced by another collection.
func trimPrefix<Prefix>(Prefix
) Removes the initial elements that satisfy the given predicate from the start of the sequence.
Available in _StringProcessing
where Self.SubSequence == Substring
Members
func replace<Output, Replacement>(some RegexComponent, maxReplacements: Int, with: (Regex<Output>.Match) throws -> Replacement
) rethrows Replaces all occurrences of the sequence matching the given regex with a given collection.
func replace<Replacement>(some RegexComponent, with: Replacement, maxReplacements: Int
) Replaces all occurrences of the sequence matching the given regex with a given collection.
func replacing<Output, Replacement>(some RegexComponent, maxReplacements: Int, with: (Regex<Output>.Match) throws -> Replacement
) rethrows -> Self Returns a new collection in which all occurrences of a sequence matching the given regex are replaced by another collection.
func replacing<Output, Replacement>(some RegexComponent, subrange: Range<Self.Index>, maxReplacements: Int, with: (Regex<Output>.Match) throws -> Replacement
) rethrows -> Self Returns a new collection in which all occurrences of a sequence matching the given regex are replaced by another regex match.
func replacing<Replacement>(some RegexComponent, with: Replacement, maxReplacements: Int
) -> Self Returns a new collection in which all occurrences of a sequence matching the given regex are replaced by another collection.
func replacing<Replacement>(some RegexComponent, with: Replacement, subrange: Range<Self.Index>, maxReplacements: Int
) -> Self Returns a new collection in which all occurrences of a sequence matching the given regex are replaced by another collection.
Available in RegexBuilder
where Self:BidirectionalCollection, Self.SubSequence == Substring
Members
func replace<Output, Replacement>(maxReplacements: Int, content: () -> some RegexComponent, with: (Regex<Output>.Match) throws -> Replacement
) rethrows Replaces all matches for the regex in this collection, using the given closures to create the replacement and the regex.
func replace<Replacement>(with: Replacement, maxReplacements: Int, content: () -> some RegexComponent
) Replaces all matches for the regex in this collection, using the given closure to create the regex.
func replacing<Output, Replacement>(maxReplacements: Int, content: () -> some RegexComponent, with: (Regex<Output>.Match) throws -> Replacement
) rethrows -> Self Returns a new collection in which all matches for the regex are replaced, using the given closures to create the replacement and the regex.
func replacing<Output, Replacement>(subrange: Range<Self.Index>, maxReplacements: Int, content: () -> some RegexComponent, with: (Regex<Output>.Match) throws -> Replacement
) rethrows -> Self Returns a new collection in which all matches for the regex are replaced, using the given closures to create the replacement and the regex.
func replacing<Replacement>(with: Replacement, maxReplacements: Int, content: () -> some RegexComponent
) -> Self Returns a new collection in which all matches for the regex are replaced, using the given closure to create the regex.
func replacing<Replacement>(with: Replacement, subrange: Range<Self.Index>, maxReplacements: Int, content: () -> some RegexComponent
) -> Self Returns a new collection in which all matches for the regex are replaced, using the given closure to create the regex.
func trimPrefix(() -> some RegexComponent
) Removes the initial elements matching the regex from the start of this collection, if the initial elements match, using the given closure to create the regex.
Available in Cxx
Members
init<C>(C
) Creates a collection containing the elements of a C++ container.
Available in Foundation
Subtypes
Extension in AsyncAlgorithms
Members
init<Source>(Source
) async rethrows Creates a new instance of a collection containing the elements of an asynchronous sequence.