MutableCollection
A collection that supports subscript assignment.
protocol MutableCollection<Element> : Collection where Self.SubSequence : MutableCollection
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
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 Element
associatedtype Index
associatedtype SubSequence
subscript(Range
<Self.Index>) -> Self.SubSequence Accesses a contiguous subrange of the collection’s elements.
subscript(Self
.Index) -> Self.Element Accesses the element at the specified position.
func partition(by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.
func swapAt(Self
.Index, Self.Index) Exchanges the values at the specified indices of the collection.
func withContiguousMutableStorageIfAvailable<R>((inout UnsafeMutableBufferPointer<Self.Element>) throws -> R
) rethrows -> R? Executes a closure on the collection’s contiguous storage.
Citizens in Swift
where Self:BidirectionalCollection
Members
func reverse(
) Reverses the elements of the collection in place.
Citizens in Swift
where Self:RandomAccessCollection
Members
func shuffle(
) Shuffles the collection in place.
func shuffle<T>(using: inout T
) Shuffles the collection in place, using the given generator as a source for randomness.
func sort(by: (Self.Element, Self.Element) throws -> Bool
) rethrows Sorts the collection in place, using the given predicate as the comparison between elements.
Citizens in Swift
where Self:RandomAccessCollection, Self.Element:Comparable
Members
func sort(
) Sorts the collection in place.
Available in Foundation
Subtypes
Extension in Algorithms
Members
func partition(subrange: Range<Self.Index>, by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Moves all elements satisfying
isSuffixElement
into a suffix of the collection, returning the start position of the resulting suffix.func rotate(subrange: Range<Self.Index>, toStartAt: Self.Index
) -> Self.Index Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.
func rotate(toStartAt: Self.Index
) -> Self.Index Rotates the elements of this collection so that the element at the specified index becomes the start of the collection.
func stablePartition(by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Moves all elements satisfying the given predicate into a suffix of this collection, preserving the relative order of the elements in both partitions, and returns the start of the resulting suffix.
func stablePartition(subrange: Range<Self.Index>, by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Moves all elements satisfying the given predicate into a suffix of the given range, preserving the relative order of the elements in both partitions, and returns the start of the resulting suffix.
Extension in Algorithms
where Self:BidirectionalCollection
Members
func partition(subrange: Range<Self.Index>, by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Moves all elements satisfying
isSuffixElement
into a suffix of the collection, returning the start position of the resulting suffix.func reverse(subrange: Range<Self.Index>
) Reverses the elements within the given subrange.
func rotate(subrange: Range<Self.Index>, toStartAt: Self.Index
) -> Self.Index Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.
func rotate(toStartAt: Self.Index
) -> Self.Index Rotates the elements of this collection so that the element at the specified index becomes the start of the collection.