Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Subscript a dictionary with an enum having a matching rawValue type without having to use .rawValue after the case
extension Dictionary {
subscript<T: RawRepresentable>(_ key: T) -> Value? where T.RawValue == Key {
return self[key.rawValue]
// Usage:
enum Type: Int {
case easy
enum Key: String {
case label
let types = [0: "🎉"]
types[Type.easy] // "🎉"
let info = ["label": "💥"]
info[Key.label] // "💥"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment