How can I use a Swift enum as a Dictionary key? Conforming to Equatable
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To use a Swift enum as a dictionary key, the enum must be Hashable, not just Equatable. Dictionary uses hashing internally to find buckets efficiently, and equality alone is not enough.
That is the key point many short answers miss. Equatable is part of the story because Hashable inherits from it, but the actual requirement for dictionary keys is Hashable.
Why Hashable Matters
A dictionary lookup does two things:
- compute a hash for the key
- compare keys for equality when hashes collide
That means a key type must support both hashing and equality. In Swift, Hashable gives you both.
This works:
For an enum with no associated values, this is all you need in modern Swift.
Enums Without Associated Values
Simple enums are the easiest case. When the cases have no associated values, Swift can synthesize Hashable conformance automatically once you declare it.
You do not need to write == or hash(into:) manually here. Synthesis is the cleanest approach.
Enums With Raw Values
Raw-value enums also work well as dictionary keys:
The raw value can help with serialization or debugging, but the dictionary still uses the enum value itself as the key.
Enums With Associated Values
Enums with associated values can also be dictionary keys as long as the associated values are themselves Hashable.
Swift can synthesize the conformance here too because Int and String are Hashable.
Manual Conformance
Manual Hashable implementation is only needed when synthesis cannot express the behavior you want. For example:
Most code should not need this, but it is useful to understand how the requirement works.
When Equatable Alone Is Relevant
You may still see discussions about Equatable because:
- '
Hashableinherits fromEquatable' - equality semantics must match hashing semantics
- old examples often mention both protocols separately
But if the concrete goal is "I want to use this enum as a dictionary key," the correct answer is still: make it Hashable.
Common Pitfalls
The biggest pitfall is trying to conform only to Equatable and expecting Dictionary to accept the enum as a key. It will not. The missing requirement is Hashable.
Another mistake is manually implementing == without keeping it consistent with hash(into:). If two keys compare equal, they must produce matching hash behavior. Breaking that rule causes subtle dictionary bugs.
Developers also over-engineer simple enums. If the enum cases and associated values are already Hashable, let Swift synthesize the implementation.
Finally, be careful if associated values contain non-hashable types. In that case, synthesis fails, and the enum cannot be used as a dictionary key unless you redesign the key representation.
Summary
- A Swift enum must be
Hashableto be used as a dictionary key. - '
Equatablealone is not enough.' - Simple enums and many enums with associated values get synthesized
Hashableconformance automatically. - Manual
hash(into:)and==are only needed in special cases. - Hashing and equality must describe the same notion of key identity.
- For most enums,
enum MyKey: Hashableis the full solution.

