美文网首页
利用runtime获取某个类的属性及方法列表

利用runtime获取某个类的属性及方法列表

作者: izsm | 来源:发表于2018-06-19 16:54 被阅读0次

    如何利用反射机制获取某个类的属性及方法列表:

    extension NSObject {
        
        /// 获取某个类的方法列表
        ///
        /// - Parameter className: 类
        /// - Returns: 某个类的方法列表
        func getMethod(className: AnyClass) -> [Selector]? {
            
            var outCount: UInt32 = 0
            guard let methods = class_copyMethodList(className, &outCount) else { return nil }
            
            var methodArray = [Selector]()
            let count = Int(outCount)
            for i in 0...(count - 1) {
                let aMet = methods[i]
                let methodName  = method_getName(aMet)
                methodArray.append(methodName)
            }
            return methodArray
        }
        
        /// 获取某个类的属性列表
        ///
        /// - Parameter className: 类
        /// - Returns: 某个类的属性列表
        func getProperty(className: AnyClass) -> [String]? {
            
            var outCount: UInt32 = 0
            guard let propers = class_copyPropertyList(className, &outCount) else { return nil }
            
            var propertyArray = [String]()
            let count: Int = Int(outCount)
            for i in 0...(count - 1) {
                let aPro = propers[i]
                if let proName = String(utf8String: property_getName(aPro)) {
                    propertyArray.append(proName)
                }
            }
            return propertyArray
        }
    }
    

    使用:

    比如修改 UIDatePicker 的文字颜色

    先调用 getProperty 方法获取属性列表:

    getProperty(className: UIDatePicker.self)
    

    打印其属性如下:

    [
    "hash",
     "superclass",
     "description",
     "debugDescription",
     "usesBlackChrome",
     "drawsBackground",
     "allowsZeroCountDownDuration",
     "allowsZeroTimeInterval",
     "dateUnderSelectionBar", 
     "contentWidth", 
     "isTimeIntervalMode",
     "useCurrentDateDuringDecoding",
     "_usesModernStyle", 
     "highlightColor",
     "textColor",
     "textShadowColor",
     "timeInterval", 
     "shouldAnimateSetDateCall",
     "datePickerMode",
     "locale", 
     "calendar", 
     "timeZone",
     "date", 
     "minimumDate",
     "maximumDate",
     "countDownDuration", 
     "minuteInterval"
    ]
    

    修改 “ textColor” 属性就可以改变文字的颜色了

    初始化UIDatePicker
    private lazy var datePicker: UIDatePicker = {
        let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 50, width: frame.size.width, height: datePickerHeight - 50))
        datePicker.backgroundColor = UIColor.black
        datePicker.datePickerMode = .date
        datePicker.locale = Locale(identifier: "zh_CN")
        datePicker.calendar = Calendar.current
        datePicker.timeZone = TimeZone.current
        datePicker.addTarget(self, action: #selector(chooseDate( _:)), for: .valueChanged)
        datePicker.setValue(UIColor.white, forKey: "textColor")
        return datePicker
    }()
    
    修改UIDatePicker文字颜色.png

    相关文章

      网友评论

          本文标题:利用runtime获取某个类的属性及方法列表

          本文链接:https://www.haomeiwen.com/subject/pjaaeftx.html