<!--
{
  "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/MutableCollection",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SM"
  },
  "title" : "MutableCollection"
}
-->

# MutableCollection

A collection that supports subscript assignment.

```
protocol MutableCollection<Element> : Collection where Self.SubSequence : MutableCollection
```

## Overview

Collections that conform to `MutableCollection` gain the ability to
change the value of their elements. This example shows how you can
modify one of the names in an array of students.

```
var students = ["Ben", "Ivy", "Jordell", "Maxime"]
if let i = students.firstIndex(of: "Maxime") {
    students[i] = "Max"
}
print(students)
// Prints "["Ben", "Ivy", "Jordell", "Max"]"
```

In addition to changing the value of an individual element, you can also
change the values of a slice of elements in a mutable collection. For
example, you can sort *part* of a mutable collection by calling the
mutable `sort()` method on a subscripted subsequence. Here’s an
example that sorts the first half of an array of integers:

```
var numbers = [15, 40, 10, 30, 60, 25, 5, 100]
numbers[0..<4].sort()
print(numbers)
// Prints "[10, 15, 30, 40, 60, 25, 5, 100]"
```

The `MutableCollection` protocol allows changing the values of a
collection’s elements but not the length of the collection itself. For
operations that require adding or removing elements, see the
`RangeReplaceableCollection` protocol instead.

# Conforming to the MutableCollection Protocol

To add conformance to the `MutableCollection` protocol to your own
custom collection, upgrade your type’s subscript to support both read
and write access.

A value stored into a subscript of a `MutableCollection` instance must
subsequently be accessible at that same position. That is, for a mutable
collection instance `a`, index `i`, and value `x`, the two sets of
assignments in the following code sample must be equivalent:

```
a[i] = x
let y = a[i]

// Must be equivalent to:
a[i] = x
let y = x
```

## Relationships

### Conforming Types

[`Array`](/documentation/Swift/Array)

[`EmptyCollection`](/documentation/Swift/EmptyCollection)

[`Values-swift.struct`](/documentation/Swift/Dictionary/Values-swift.struct)

[`UnsafeMutableBufferPointer`](/documentation/Swift/UnsafeMutableBufferPointer)

[`UnsafeMutableRawBufferPointer`](/documentation/Swift/UnsafeMutableRawBufferPointer)

[`CollectionOfOne`](/documentation/Swift/CollectionOfOne)

[`ArraySlice`](/documentation/Swift/ArraySlice)

[`ContiguousArray`](/documentation/Swift/ContiguousArray)

[`Slice`](/documentation/Swift/Slice)

### Inherits From

[`Collection`](/documentation/Swift/Collection)

[`Sequence`](/documentation/Swift/Sequence)

---

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)