<!--
{
  "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/Array/swapAt(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SMsE6swapAtyy5IndexQz_ACtF::SYNTHESIZED::s:Sa"
  },
  "title" : "swapAt(_:_:)"
}
-->

# swapAt(_:_:)

Exchanges the values at the specified indices of the collection.

```
mutating func swapAt(_ i: Self.Index, _ j: Self.Index)
```

## Parameters

`i`

The index of the first value to swap.

`j`

The index of the second value to swap.

## Discussion

Both parameters must be valid indices of the collection that are not
equal to `endIndex`. Calling `swapAt(_:_:)` with the same index as both
`i` and `j` has no effect.

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