Junior — Middle
What information can serve as a key in a dictionary data structure?
sobes.tech AI
Answer from AI
In the data structure, a dictionary's key can be any data type that conforms to the Hashable protocol in Swift. This means the key must be unique and capable of computing a hash value for quick lookup.
Examples of keys:
- Strings (
String) - Integers (
Int) - Enumerations (
enum) that conform to Hashable - Tuples consisting of Hashable types
Example:
let dict: [String: Int] = ["apple": 3, "banana": 5]
Here, the keys are strings.