在符合 UIApperance 的自定义 类 中允许使用哪些 Swift 属性 类型?

What Swift property types are allowed in UIApperance compliant custom classes?

我最近尝试将 UIApperance 兼容 属性 添加到我的 Swift 类 (ScoreView) 之一,但没有成功。您可以在下面看到相关代码:

private var gaugeThresholds = Array<GaugeThreshold?>(repeating: nil, count: ScoreView.maxGaugeIntervals)

public func gaugeThreshold(forInterval interval: Int) -> GaugeThreshold? {
    return (0 <= interval && interval < ScoreView.maxGaugeIntervals) ? self.gaugeThresholds[interval] : nil
}

public func setGaugeThreshold(_ threshold: GaugeThreshold, forInterval interval: Int) {
    if 0 <= interval && interval < ScoreView.maxGaugeIntervals {
        self.gaugeThresholds[interval] = threshold
    }
}

其中 GaugeThreshold 是一个 Swift 结构:

public struct GaugeThreshold {
    let until: Float
    let color: UIColor

    public init(until: Float, color: UIColor) {
        self.until = until
        self.color = color
    }
}

这失败得很惨,我能想到的失败的唯一原因是我为 属性 使用自定义类型而不是 之一标准类型.

根据 UIAppearanceContainer 文档:属性 类型可以是任何标准 iOS 类型:idNSIntegerNSUIntegerCGFloatCGPointCGSizeCGRectUIEdgeInsetsUIOffset。但是,如果您浏览 list of methods and properties conforming to UIAppearance as of iOS 8.0,您可以看到还有其他类型可以正常工作,例如 UIImageUIColor... 所以有人知道是什么类型可以有效地用作一个 UIAppearance 属性?

当然,没有什么比发帖到 Whosebug 五分钟后找出答案更好的了。原来关键是 id 类型。改变

public struct GaugeThreshold { ...

public class GaugeThreshold: NSObject { ...

成功了。