Get all keys for a class
I am recently working with a function to change the text color like this:func setValue(_ value: AnyObject?, forKey key: String)
NSKeyValueCoding
UIPickerDate
class ColoredDatePicker: UIDatePicker {
var changed = false
override func addSubview(view: UIView) {
if !changed {
changed = true
self.setValue(UIColor(red: 0.42, green: 0.42, blue: 0.42, alpha: 1), forKey: "textColor")
}
super.addSubview(view)
}
}
Regarding the answer in this question . It works great.
But here's my answer:
How do I find out the names the class provides as textColor
used above ?.
I'm trying to find anything to get all the names or documentation, but so far I haven't found anything yet to get the keys provided by the class like in the above case.
source to share
The objective-c runtime provides this type of reflection for properties:
id UIDatePickerClass = objc_getClass("UIDatePicker");
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(UIDatePickerClass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}
edit - swift also has main reflection: fooobar.com/questions/25655 / ...
source to share
For anyone looking for a Swift solution like @Adam here:
var propertiesCount : CUnsignedInt = 0
let propertiesInAClass = class_copyPropertyList(UIDatePicker.self, &propertiesCount)
var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()
for var i = 0; i < Int(propertiesCount); i++ {
var property = propertiesInAClass[i]
var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding)
println(propName)
}
source to share