<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RangeReplaceableCollection",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sm"
  },
  "title" : "RangeReplaceableCollection"
}
-->

# 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
```

## 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
`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.

## Topics

### Creating a New Collection

[`init()`](/documentation/Swift/RangeReplaceableCollection/init())

Creates a new, empty collection.

### Adding Elements

[`insert(contentsOf:at:)`](/documentation/Swift/RangeReplaceableCollection/insert(contentsOf:at:))

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)