将协议一致性添加到现有 class 然后检查是否实现?

Adding protocol conformance to existing class then checking if implements?

我正在尝试将几个现有的 类 分组到一个自定义协议中,以便我可以对它们一视同仁。例如,我想将这两个 类 组合在一个协议下,如下所示:

protocol CLKComplicationTemplateRingable {
    var fillFraction: Float { get set }
}

extension CLKComplicationTemplateCircularSmallRingText: CLKComplicationTemplateRingable {

}

extension CLKComplicationTemplateModularSmallRingText: CLKComplicationTemplateRingable {

}

为什么我做的时候做不到:

if let template as? CLKComplicationTemplateRingable {
    print("\(template.fillFraction)")
}

它没有编译,它给出了这个错误:Variable binding in a condition requires an initializer

我的做法是否正确?任何建议或帮助将不胜感激!

这样做:

if template is CLKComplicationTemplateRingable {
    print("\(template.fillFraction)")
}

"if let" 变体为:

if let template = template as? CLKComplicationTemplateRingable {
    print("\(template.fillFraction)")
}