我有一份文件。它在“产品”中有一堆对象字段如何遍历这个字典?
let dict = doc.data()
for (key,value) in dict["products"]{
}
这给了我错误:
输入'Any?'不符合协议'序列'
我明显的问题是什么?
编辑:文档说
/**
* 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
你需要把它投到Dictionary
。