Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created September 18, 2015 13:18
Show Gist options
  • Save rizumita/ed0e1bac3b72e31a107d to your computer and use it in GitHub Desktop.
Save rizumita/ed0e1bac3b72e31a107d to your computer and use it in GitHub Desktop.
Get value of property of class or struct by key as string
func get<T>(sv: T, key: String) -> Any? {
let mirror = Mirror(reflecting: sv)
for (name, value) in mirror.children where name == key {
return value
}
return nil
}
get(s, key: "a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment