swift - 使用 Swift 遍历 firestore 中的对象字段

标签 swift google-cloud-firestore

我有一份文件。它在“产品”中有一堆对象字段 我如何遍历这本字典?

let dict = doc.data()

for (key,value) in dict["products"]{

}

这给了我错误:

Type 'Any?' does not conform to protocol 'Sequence'

我的明显问题是什么?

编辑:文档说

/**
* Retrieves all fields in the document as an `NSDictionary`.
*
* @return An `NSDictionary` containing all fields in the document.
*/
- (NSDictionary<NSString *, id> *)data;

最佳答案

你应该使用:

let dict = doc.data()

if let products = dict["products"] as? [AnyHashable: Any] {

    for (key,value) in products {

    }
}

doc.data() 返回 Any?,因此为了将其视为 Dictionary,您需要将其转换为 字典

关于swift - 使用 Swift 遍历 firestore 中的对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051659/

相关文章:

swift - Xcode: 没有这样的模块 'FacebookLogin'

ios - 在 Swift 中使用 Firebase - pod 安装错误

firebase - 如何使用 Firestore 获取 `Cloud Functions shell`

android - 为什么我得到一个空对象引用 documentSnapshot.toObject() 方法?

Firebase - Firestore - 我会阅读文档多少次

ios - 将文档的文档 ID 添加到它自己的 Firestore 文档中 - Swift 4

ios - 将单元格的 subview 快速移动到前面

ios - 获取不可见的 CollectionView 单元格

ios - 允许子类使用子类委托(delegate)

ios - iOS >= 8 使用 os_log 和 NSLog 的快速日志记录