Created
October 5, 2017 09:38
-
-
Save daehn/3632a2e6eeb7461ce8efa1e5fae22da1 to your computer and use it in GitHub Desktop.
Subscript a dictionary with an enum having a matching rawValue type without having to use .rawValue after the case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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