如何声明Dictionary <string,decimal =“”>符合协议

时间:2017-04-27 18:16:09

标签: swift dictionary protocols

我已经定义了一个协议:

public protocol VariableTable {
    subscript(key:String) -> Decimal? { get set }
}

仅表示VariableTable必须为String-&gt; Decimal提供下标运算符。

显然,Dictionary<String, Decimal>符合该要求。我如何让编译器知道?

extension Dictionary<String, Decimal> : VariableTable {}

的产率:

Constrained extension must be declared on the unspecialized generic type 'Dictionary' with constraints specified by a 'where' clause

其中:

extension Dictionary : VariableTable where Key == String, Value == Decimal {}

或:

 extension Dictionary : VariableTable where Element == (String, Decimal) {}

导致错误:

Extension of type 'Dictionary' with constraints cannot have an inheritance clause

1 个答案:

答案 0 :(得分:1)

这在Swift 3.0中是不可能的。

但是如果你关心的只是拥有这个EmpID TotalRating IsEMPTL --------------------------------- 1 13 YES 5 4 YES 下标,你可以将字典包装成符合协议的另一种类型:

VariableTable