当我想检查一个类型是否符合简单协议时,我可以使用以下代码:
if let type = ValueType.self as? Codable.Type {}
当协议有关联类型时,例如RawRepresentable
有RawValue
, 当我这样做:
if let type = ValueType.self as? RawRepresentable.Type {}
编译器会显示以下错误:
“协议‘RawRepresentable’只能用作通用约束,因为它具有Self或相关类型要求”。
那么如何检查符合带有关联类型的协议呢?