<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UniqueArray/formIndex(_:offsetBy:limitedBy:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11UniqueArrayVsRi_zrlE9formIndex_8offsetBy07limitedF0ySiz_SizSitF"
  },
  "title" : "formIndex(_:offsetBy:limitedBy:)"
}
-->

# formIndex(_:offsetBy:limitedBy:)

Offsets the given index by the specified distance, but no further than
the given limiting index.

```
func formIndex(_ index: inout UniqueArray<Element>.Index, offsetBy n: inout Int, limitedBy limit: UniqueArray<Element>.Index)
```

## Parameters

`index`

A valid index of the array. On return, `index` is
set to the resulting position.

`n`

The distance to offset `index`.
On return, `n` is set to zero if the operation succeeded without
hitting the limit; otherwise, `n` reflects the number of steps that
couldn’t be taken.

`limit`

A valid index of the array to use as a limit.
If `n > 0`, a limit that is less than `index` has no effect.
Likewise, if `n < 0`, a limit that is greater than `index` has no
effect.

## Discussion

If the operation was able to offset `index` by exactly the requested
number of steps without hitting `limit`, then on return `n` is set to `0`,
and `index` is set to the adjusted index.

If the operation hits the limit before it can take the requested number
of steps, then on return `index` is set to `limit`, and `n` is set
to the number of steps that couldn’t be taken.

The value passed as `n` must not offset `index` beyond the bounds of the
container, unless the index passed as `limit` prevents offsetting beyond
those bounds.

> Note: To improve performance, this method does not validate that the
> given index is valid before offseting it. Index validation is
> deferred until the resulting index is used to access an element.
> This optimization may be removed in future versions; do not rely on it.

> 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)