我正在尝试让特定的字典类型符合协议(protocol)。
typealias FirebaseDictionary = Dictionary<String, FirebaseValue>
我希望符合 FirebaseValue
协议(protocol)
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 {
}
但如果可能的话,我无法找出使它符合协议(protocol)的正确语法。如果不可能,还有其他方法可以达到同样的效果吗?我试图只允许特定类型进入属性,并且能够在回读时轻松辨别它们是什么类型。
This question was asked但没有给出明确的答案,无论如何它可能已经改变
最佳答案
从 Swift 4.2 开始,您可以使用:
extension Dictionary : FirebaseValue where Key == String, Value == FirebaseValue {
}
关于swift - 约束字典的协议(protocol)扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44663049/