约束字典的协议扩展

时间:2017-06-20 21:11:10

标签: swift dictionary generics swift-protocols swift-dictionary

我正在尝试获取特定的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但未给出明确的答案,无论

,它都可能已发生变化

1 个答案:

答案 0 :(得分:1)

从Swift 4.2开始,您可以执行以下操作:

 your_async_method () async {

     final documents = await db.collection('users').where("email", isEqualTo: "jtent@mail.com").getDocuments();
     final userObject = documents.documents.first.data;
     print(userObject);
 }
相关问题