Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created February 18, 2017 19:26
Show Gist options
  • Save wh1pch81n/8450d607773adb185bbf128eeb46ce19 to your computer and use it in GitHub Desktop.
Save wh1pch81n/8450d607773adb185bbf128eeb46ce19 to your computer and use it in GitHub Desktop.
enum Planets: String {
case Mercury
case Venus
case Earth
}
// Add protocol to enums that need it
public protocol UsesRawValue {
var rawValue: String { get }
}
extension Planets: UsesRawValue {}
extension Dictionary where Key: ExpressibleByStringLiteral {
subscript(key: UsesRawValue) -> Value? {
get { return self[key.rawValue as! Key] }
set { self[key.rawValue as! Key] = newValue }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment