Swift 对属性使用getter/setter使其不会出现在反射(镜像)中

Swift 对属性使用getter/setter使其不会出现在反射(镜像)中,swift,reflection,getter-setter,mirror,Swift,Reflection,Getter Setter,Mirror,我一直在尝试为我的自定义组件实现一个主题逻辑。我将使用ZFButton作为示例 当应用程序启动时,我实例化ZFbutton并设置我希望这一主题具有的任何特征: let theme = ZFButton() theme.backgroundColor = .red //UIButton property theme.cornerRadius = 8 //ZFButton property theme.borderColor = .green //ZFButton property theme.bo

我一直在尝试为我的自定义组件实现一个主题逻辑。我将使用ZFButton作为示例

当应用程序启动时,我实例化ZFbutton并设置我希望这一主题具有的任何特征:

let theme = ZFButton()
theme.backgroundColor = .red //UIButton property
theme.cornerRadius = 8 //ZFButton property
theme.borderColor = .green //ZFButton property
theme.borderWidth = 1 //ZFButton property
然后将其添加到主题数组中:

ZFButton.themes.append(theme)
它位于ZF按钮中,如下所示:

public static var themes = [ZFButton]()
在我的ZFButton中,我有以下属性,它允许我从IB属性检查器中选择要用于特定ZFButton的主题

@IBInspectable public var theme: Int = 0 { didSet { self.setupTheme() } }
最后,一旦设置了theme属性,就会调用setupTheme(),在这个过程中,我尝试将给定主题的所有属性中的值集复制到ZFButton的这个特定实例。为此,我使用反射:

private func setupTheme() {
    
    if ZFButton.themes.count > self.theme {
        
        let theme = ZFButton.themes[self.theme]
        let mirror = Mirror(reflecting: theme)

        for child in mirror.children {
            
            if let label = child.label,
               label != "theme", //check to prevent recursive calls
               self.responds(to: Selector(label)) {
                   
                self.setValue(child.value, forKey: label)
                
                print("Property name:", child.label)
                print("Property value:", child.value)
            }
        }
    }
}
现在我有两个问题:

1-反射中不显示具有setter/getter的属性,例如:

@IBInspectable public var borderColor: UIColor {
    
    set { layer.borderColor = newValue.cgColor }
    get { return UIColor(cgColor: layer.borderColor!) }
}
而使用didSet的属性则可以,例如:

@IBInspectable public var iconText: String = "" { didSet { self.setupIcon() } }
但是,我确实需要一个getter来返回
中的
borderColor


2-当使用Mirror来反映所有ZFButton属性时,除了(1)中描述的问题外,我也没有得到UIButton属性,有没有办法也得到ZFButton的超类(UIButton)属性?

对于第一个问题,我使用了以下扩展,它确实看到了与Mirror不同的getter/setter属性:

extension NSObject {
  
    func propertiesNames() -> [String] {
        
        var count : UInt32 = 0
        let classToInspect = type(of: self)

        guard let properties : UnsafeMutablePointer <objc_property_t> = class_copyPropertyList(classToInspect, &count) else { return [] }

        var propertyNames : [String] = []

        let intCount = Int(count)

        for i in 0..<intCount {

            let property : objc_property_t = properties[i]
            let propertyName = NSString(utf8String: property_getName(property))!

            propertyNames.append(propertyName as String)
        }

        free(properties)

        return propertyNames
    }
}
扩展对象{ func propertiesNames()->[字符串]{ 变量计数:UInt32=0 让classToInspect=类型(of:self) guard let属性:unsafemeutablepointer=class_copyPropertyList(classToInspect,&count)else{return[]} 变量propertyNames:[字符串]=[] 让intCount=Int(count) 因为我在0。。