swift - 对属性使用 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() } }
但是,我确实需要一个吸气剂来返回borderColor
in layer
。
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
}
}
至于第二个问题,我最终将每个属性从主题复制到按钮,因为它们总是相同的。目标是避免每次在 ZFButton 中实现新事物时都必须维护一个 Theme 类来桥接值。
推荐阅读
- azure-sql-data-warehouse - 为什么 CTE 在 Sql 数据仓库中的性能比临时表好得多?
- html - Signaturepad 清除属性未在模式引导对话框中清除
- angular - Angular 8 边缘案例:ActivatedRoute#data 上的奇怪值(在父级上解析,在子级上的数据 => 子级数据被覆盖)
- pandas - Pandas groupby year 按 n 个最大值过滤数据框
- regex - RewriteCond REQUEST_URI 不匹配整个路径
- gradle - 如何从 ShadowJar 的配置文件夹中排除所有文件
- apache-spark - NameError:名称“split”未使用 Spark 定义
- c# - 如何正确检查 ILogger.LogCritical 是否已使用 NSubstitute 调用?
- bash - 为什么`bind -X`显示非活动绑定?
- c# - 两个布尔值列表上的逻辑或