Search code examples
iosswiftkey-value-coding

Get all the keys for a class


Recently I work with the function func setValue(_ value: AnyObject?, forKey key: String) of the NSKeyValueCoding protocol to change the text color of a UIPickerDate in the following way:

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 perfectly.

But my answer here is:

How do I know what names that the class provide like the textColor used above?.

I'm trying to find anything get all the names or in the documentation, but so far I don't find anything yet to get the keys provided by a class like in the above case.


Solution

  • 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));
    }
    

    reference doc: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html#//apple_ref/c/func/class_copyPropertyList

    edit - swift has basic reflection too: https://stackoverflow.com/a/24069875/1385467