Created
March 9, 2019 20:28
-
-
Save phucnm/79479103a46400023cd40471cfb70ac9 to your computer and use it in GitHub Desktop.
MirrorableEnum
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
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