<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/DiscontiguousSlice/subscript(_:)-1j7n7",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s18DiscontiguousSliceVsSMRzrlEy7ElementQzAB5IndexVyx_Gcip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the element at the specified position.

```
subscript(i: DiscontiguousSlice<Base>.Index) -> Base.Element { get set }
```

## Parameters

`i`

The position of the element to access. `i`
must be a valid index of the collection that is not equal to the
`endIndex` property.

## Overview

For example, you can replace an element of an array by using its
subscript.

```
var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
print(streets[1])
// Prints "Butler"
```

You can subscript a collection with any valid index other than the
collection’s end index. The end index refers to the position one
past the last element of a collection, so it doesn’t correspond with an
element.

> Complexity: O(1)

---

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)