首页 > 解决方案 > 对属性使用 getter/setter 使其不会出现在反射(镜像)中

问题描述

我一直在尝试为我的自定义组件实现主题逻辑。我将使用 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)

它位于 ZFButton 中,如下所示:

public static var themes = [ZFButton]()

在我的 ZFButton 中,我有以下属性,它允许我从 IB 属性检查器中选择我想为那个特定的 ZFButton 使用哪个主题

@IBInspectable public var theme: Int = 0 { didSet { self.setupTheme() } }

最后,一旦设置了主题属性,就会调用 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() } }

但是,我确实需要一个吸气剂来返回borderColorin layer

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

标签: swiftreflectiongetter-settermirror

解决方案


对于第一个问题,我最终使用了以下扩展,它确实看到了与 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
    }
}

至于第二个问题,我最终将每个属性从主题复制到按钮,因为它们总是相同的。目标是避免每次在 ZFButton 中实现新事物时都必须维护一个 Theme 类来桥接值。


推荐阅读