<!--
{
  "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/Dictionary/index(forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SD5index6forKeySD5IndexVyxq__GSgx_tF"
  },
  "title" : "index(forKey:)"
}
-->

# index(forKey:)

Returns the index for the given key.

```
func index(forKey key: Key) -> Dictionary<Key, Value>.Index?
```

## Parameters

`key`

The key to find in the dictionary.

## Return Value

The index for `key` and its associated value if `key` is in
the dictionary; otherwise, `nil`.

## Discussion

If the given key is found in the dictionary, this method returns an index
into the dictionary that corresponds with the key-value pair.

```
let countryCodes = ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"]
let index = countryCodes.index(forKey: "JP")

print("Country code for \(countryCodes[index!].value): '\(countryCodes[index!].key)'.")
// Prints "Country code for Japan: 'JP'."
```

---

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)