约束字典的协议扩展

Protocol Extension of Constrained Dictionary

我正在尝试获取特定的字典类型以符合协议。

typealias FirebaseDictionary = Dictionary<String, FirebaseValue>

我希望符合 FirebaseValue 协议

protocol FirebaseValue {
    // stuff here
}

我试过了

extension FirebaseDictionary: FirebaseValue {

}

但是我得到一个错误 Constrained extension must be declared on the unspecialized generic type 'Dictionary' with constraints specified by a 'where' clause。所以我现在有了这个

extension Dictionary where Key == String, Value == FirebaseValue {

}

但如果可能的话,我无法找出使它符合协议的正确语法。如果不可能,还有其他方法可以达到同样的效果吗?我试图只允许特定类型进入 属性,并且能够在回读时轻松辨别它们是什么类型。

This question was asked 但没有给出明确的答案,而且可能已经发生了变化

从 Swift 4.2 开始,您可以使用:

extension Dictionary : FirebaseValue where Key == String, Value == FirebaseValue {
}