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

时间:2017-06-08 07:26:13

标签: ios swift uiappearance

我最近尝试过将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类型:idNSIntegerNSUIntegerCGFloat,{{1} },CGPointCGSizeCGRectUIEdgeInsets。但是,如果您浏览list of methods and properties conforming to UIAppearance as of iOS 8.0,就可以看到其他类型正常工作为UIOffsetUIImage ...所以有人知道什么使类型有效用作一个UIColor属性?

1 个答案:

答案 0 :(得分:0)

当然,没有什么比发帖到StackOverflow五分钟后找到答案了。事实证明,关键是id类型。改变

public struct GaugeThreshold { ...

public class GaugeThreshold: NSObject { ...

做了这个伎俩。