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

# BidirectionalCollection

A collection that supports backward as well as forward traversal.

```
protocol BidirectionalCollection<Element> : Collection where Self.Indices : BidirectionalCollection, Self.SubSequence : BidirectionalCollection
```

## Overview

Bidirectional collections offer traversal backward from any valid index,
not including a collection’s `startIndex`. Bidirectional collections can
therefore offer additional operations, such as a `last` property that
provides efficient access to the last element and a `reversed()` method
that presents the elements in reverse order. In addition, bidirectional
collections have more efficient implementations of some sequence and
collection methods, such as `suffix(_:)`.

# Conforming to the BidirectionalCollection Protocol

To add `BidirectionalProtocol` conformance to your custom types, implement
the `index(before:)` method in addition to the requirements of the
`Collection` protocol.

Indices that are moved forward and backward in a bidirectional collection
move by the same amount in each direction. That is, for any valid index `i`
into a bidirectional collection `c`:

- If `i >= c.startIndex && i < c.endIndex`, then
  `c.index(before: c.index(after: i)) == i`.
- If `i > c.startIndex && i <= c.endIndex`, then
  `c.index(after: c.index(before: i)) == i`.

Valid indices are exactly those indices that are reachable from the
collection’s `startIndex` by repeated applications of `index(after:)`, up
to, and including, the `endIndex`.

## Relationships

### Conforming Types

[`Words-swift.struct`](/documentation/Swift/UInt8/Words-swift.struct)

[`Ranges-swift.struct`](/documentation/Swift/RangeSet/Ranges-swift.struct)

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

[`UTF8View`](/documentation/Swift/Unicode/Scalar/UTF8View)

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

[`Words-swift.struct`](/documentation/Swift/Int64/Words-swift.struct)

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

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

[`Words-swift.struct`](/documentation/Swift/UInt16/Words-swift.struct)

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

[`UTF16View`](/documentation/Swift/Substring/UTF16View)

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

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

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

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

[`Words-swift.struct`](/documentation/Swift/Int16/Words-swift.struct)

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

[`Words-swift.struct`](/documentation/Swift/Int32/Words-swift.struct)

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

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

[`UnicodeScalarView`](/documentation/Swift/String/UnicodeScalarView)

[`Words-swift.struct`](/documentation/Swift/UInt/Words-swift.struct)

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

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

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

[`Words-swift.struct`](/documentation/Swift/Int8/Words-swift.struct)

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

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

[`Words-swift.struct`](/documentation/Swift/UInt64/Words-swift.struct)

[`UTF16View`](/documentation/Swift/String/UTF16View)

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

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

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

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

[`UTF8View`](/documentation/Swift/String/UTF8View)

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

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

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

[`UTF16View`](/documentation/Swift/Unicode/Scalar/UTF16View)

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

[`UnicodeScalarView`](/documentation/Swift/Substring/UnicodeScalarView)

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

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

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

[`Words-swift.struct`](/documentation/Swift/UInt32/Words-swift.struct)

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

[`Words-swift.struct`](/documentation/Swift/Int/Words-swift.struct)

[`Words-swift.struct`](/documentation/Swift/UInt128/Words-swift.struct)

[`UTF8View`](/documentation/Swift/Substring/UTF8View)

### Inherited By

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

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

### Inherits From

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

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

---

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)