Returns the index for the given key.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
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'."