A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.
- Xcode 8.0+
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
Range protocol defines.
Range types can remove one or more elements using a single operation.
Lastly, use the eponymous
replace 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
Conforming to the RangeReplaceableCollection Protocol
Range conformance to your custom collection, add an empty initializer and the
replace method to your custom type.
Range provides default implementations of all its other methods using this initializer and method. For example, the
remove method is implemented by calling
replace with an empty collection for the
new parameter. You can override any of the protocol’s required methods to provide your own custom implementation.