Skip to content

Instantly share code, notes, and snippets.

@phucnm
Created March 9, 2019 20:28
Show Gist options
  • Save phucnm/79479103a46400023cd40471cfb70ac9 to your computer and use it in GitHub Desktop.
Save phucnm/79479103a46400023cd40471cfb70ac9 to your computer and use it in GitHub Desktop.
MirrorableEnum
protocol MirrorableEnum {}
extension MirrorableEnum {
var mirror: (label: String, params: [String: Any]) {
get {
let reflection = Mirror(reflecting: self)
guard reflection.displayStyle == .enum,
let associated = reflection.children.first else {
return ("\(self)", [:])
}
let values = Mirror(reflecting: associated.value).children
var valuesArray = [String: Any]()
for case let item in values where item.label != nil {
valuesArray[item.label!] = item.value
}
return (associated.label!, valuesArray)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment