Created
May 21, 2016 18:13
-
-
Save LeonardoCardoso/84c4537653ea8d020e052db02a099cd0 to your computer and use it in GitHub Desktop.
Get any class properties using reflection
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
// 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