Skip to content

Instantly share code, notes, and snippets.

@LeonardoCardoso
Created May 21, 2016 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonardoCardoso/84c4537653ea8d020e052db02a099cd0 to your computer and use it in GitHub Desktop.
Save LeonardoCardoso/84c4537653ea8d020e052db02a099cd0 to your computer and use it in GitHub Desktop.
Get any class properties using reflection
// MARK: - Get properties using reflection
extension NSObject {
func properties() -> [String: String]{
var results: [String: String] = [:]
for child in Mirror(reflecting: self).children {
var propertyName = ""
var propertyValue = ""
NSLog("\(child)")
if let name = child.label {
propertyName = name
}
// Change UILabel for whatsoever object type you want
if let label: UILabel = child.value as? UILabel {
if let value: String = label.text {
propertyValue = value
}
}
results.updateValue(propertyValue, forKey: propertyName)
}
return results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment