我有一份文件。它在“产品”中有一堆对象字段 我如何遍历这本字典?
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/