【发布时间】:2018-01-16 02:49:44
【问题描述】:
我正在使用 Firestore(仍处于测试阶段),我的数据结构如下:
我想以字符串的形式获取每种成分,以使用它们的名称创建成分类的对象。这是我正在使用的方法:
private func loadIngredients(){
let db = Firestore.firestore()
let recipeCollectionRef = db.collection("dishes").document((recipe?.name)!)
recipeCollectionRef.getDocument { (document, error) in
if let document = document {
print("Document data: \(document.data())")
print("\(document.data().values)")
for value in document.data().values {
print("Value: \(value)")
print(type(of: value))
}
} else {
print("Document does not exist")
}
}
}
这目前产生:
文档数据:[“成分”:<__nsarraym>( 香蕉, 麦片 ) ,“级别”:容易] LazyMapCollection, Any>(_base: ["ingredients": <__nsarraym>( 香蕉, 麦片 ) , "级别": 容易], _transform: (函数)) 价值: ( 香蕉, 麦片 ) __NSArrayM 价值:容易 NSTaggedPointerString
根据我阅读 Apple 关于字典的文档的理解: 我的字典的类型为 [String: Any],在这种情况下,“成分”是充当键的字符串,值可以是任何对象。我正在使用的数组是一个可变数组。
对于如何获取该数组及其各自的元素,我感到非常困惑。我尝试从 LazyMapCollection 转换为 String ,但这会将整个数组生成为字符串。我还尝试通过键“成分”访问该值,但这不能作为“无法使用String 类型的索引下标LazyMapCollection<[String : Any], Any> 类型的值
【问题讨论】:
标签: arrays swift firebase google-cloud-firestore