2015-06-17 29 views
3

最近,我與NSKeyValueCoding協議的功能func setValue(_ value: AnyObject?, forKey key: String)改變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) 
    } 
} 

關於以此question答案。它完美的作品。

但在這裏我的回答是:

我怎麼知道這個類提供什麼樣的名字,像上面所用的textColor

我試圖找到任何東西得到所有的名稱或文檔中,但到目前爲止,我沒有發現任何尚未得到一類在上述情況下提供像

回答

4

Objective-C的運行提供了性能研究此問題:

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

參考文檔:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html#//apple_ref/c/func/class_copyPropertyList

編輯 - SWIFT已基本反射太:https://stackoverflow.com/a/24069875/1385467

+0

感謝您的提示,我必須改變很多代碼,但無關緊要的方向就是這個問題。 –

2

對於那些我們希望有一個像@Adam這樣的快速解決方案,這裏是:

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