使用Swift循环遍历firestore中的对象字段

问题描述 投票:0回答:1

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

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;
swift google-cloud-firestore
1个回答
1
投票

你应该使用:

let dict = doc.data()

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

    for (key,value) in products {

    }
}

doc.data()返回Any?,所以为了把它当作Dictionary你需要把它投到Dictionary

© www.soinside.com 2019 - 2024. All rights reserved.