Arrays 如何提取uu nsarray中的每个元素?

Arrays 如何提取uu nsarray中的每个元素?,arrays,swift,firebase,google-cloud-firestore,Arrays,Swift,Firebase,Google Cloud Firestore,我正在使用Firestore(仍处于测试阶段),我的数据结构如下: private func loadIngredients(){ let db = Firestore.firestore() let recipeCollectionRef = db.collection("dishes").document((recipe?.name)!) recipeCollectionRef.getDocument { (document, error) in if

我正在使用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")
        }
    }
}

我希望以字符串形式获取每个成分,以创建成分类的对象及其名称。这是我正在使用的方法:

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")
        }
    }
}
这目前产生:

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")
        }
    }
}
文件数据:[“成分”:( 香蕉, 燕麦粥 ) ,“级别”:简单] LazyMapCollection,任意>(_碱:[“成分”: ( 香蕉, 燕麦粥 ) ,“级别”:简单],_变换:(函数)) 价值:( 香蕉, 燕麦粥 ) __恩萨雷姆 价值:简单 NSTaggedPointerString

根据我阅读苹果字典文档的理解: 我的字典有[String:Any]类型,在本例中,“配料”是充当键的字符串,值可以是任何对象。我使用的数组是可变数组


我对如何获得该数组及其各自的元素感到非常困惑。我曾尝试将LazyMapCollection转换为字符串,但这会将整个数组转换为字符串。我还尝试通过键“成分”访问该值,但这不起作用,因为“不能用
String
类型的索引下标
LazyMapCollection
类型的值,
document.data()
是一个字典(
[String:Any]

因此,首先:

if let document = document, let data = document.data() as? [String:Any] {
}
现在,如果要访问配料数组,请执行以下操作:

if let ingredients = data["ingredients"] as? [String] {
}
所有这些都是:

private func loadIngredients(){
    let db = Firestore.firestore()
    let recipeCollectionRef = db.collection("dishes").document((recipe?.name)!)
    recipeCollectionRef.getDocument { (document, error) in
        if let document = document, let data = document.data() as? [String:Any] {
            // Get the ingredients array
            if let ingredients = data["ingredients"] as? [String] {
                // do whatever you need with the array
            }

            // Get the "easy" value
            if let east = data["easy"] as? String {
            }
        } else {
            print("Document or its data does not exist")
        }
    }

谢谢你的回答。我现在刚刚有机会测试一下。我现在可以打印出每个成分对象的名称,这意味着你的解决方案有效。如果我想启动同时获取多个属性的对象。你的代码的这一部分可以:if让成分=数据[“成分”]作为?[String]{//对数组执行任何需要的操作}将扩展到此?如果让配料=数据[“配料”]as?[String],让ease=数据[“简单”]as?String{//processing}