【问题标题】:Get all the keys for a class获取课程的所有密钥
【发布时间】:2015-06-17 15:11:22
【问题描述】:

最近我使用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?。

我试图找到任何东西来获取所有名称或文档,但到目前为止,我还没有找到任何东西来获取上述案例中类提供的 keys

【问题讨论】:

    标签: ios swift key-value-coding


    【解决方案1】:

    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

    edit - swift 也有基本的反射:https://stackoverflow.com/a/24069875/1385467

    【讨论】:

    • 感谢您的提示,我必须更改很多代码,但方向无关紧要。
    【解决方案2】:

    对于那些我们想要像@Adam 这样的 Swift 解决方案的人,这里是:

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

    斯威夫特 4.1:

    var propertiesCount: CUnsignedInt = 0
    let propertiesInAClass = class_copyPropertyList(UIDatePicker.self, &propertiesCount)!
    var propertiesDictionary: NSMutableDictionary = [:]
    
    for i in 0..<propertiesCount {
        var property = propertiesInAClass[Int(i)]
        let propName = NSString(cString: property_getName(property), encoding: String.Encoding.utf8.rawValue)
        print(propName)
    }
    

    【讨论】:

      【解决方案3】:

      对@Victor Sigler 和@dwlz 进行了一些更改,以获取所有键的当前值:

          var propertiesCount : CUnsignedInt = 0
          let propertiesInClass = class_copyPropertyList(UIDatePicker.self, &propertiesCount)
          print("Prperties:\(propertiesCount)")
          for i in 0..<propertiesCount
          {
              let property = propertiesInClass![Int(i)]
              let name = NSString(cString: property_getName(property), encoding: String.Encoding.utf8.rawValue)
              print("\(i + 1). Key:\(name!),value \(String(describing: self.value(forKey: name! as String)))")
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 2017-03-16
        • 2021-06-12
        • 2013-08-11
        相关资源
        最近更新 更多