本文介绍了如何遍历具有多个地图的Firestore文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我比较新,所以我主要使用字典,但是在这里我们需要遍历一个嵌入式结构.有了这个,我需要能够在UITableView中填充扩展单元格.

I mostly work with dictionaries since I am fairly new but here we have an embedded struct that I need to loop through. With this, I need to be able to populate expanding cells in UITableView.

我的结构如下:

struct Complain: Codable {
    let eMail, message, timeStamp, userEmail: String
    let status: Bool
    let planDetails: PlanDetails

    enum CodingKeys: String, CodingKey {
        case eMail = "E-mail"
        case message = "Message"
        case timeStamp = "Time_Stamp"
        case userEmail = "User_Email"
        case status, planDetails
    }
}

// MARK: - PlanDetails
struct PlanDetails: Codable {
    let menuItemName: String
    let menuItemQuantity: Int
    let menuItemPrice: Double
}

我的查询如下:

db.collection("Feedback_Message").getDocuments() { documentSnapshot, error in
            if let error = error {
                print("error:\(error.localizedDescription)")
            } else {
                for document in documentSnapshot!.documents {
                    let eMail = document.get("E-mail")
                    let message = document.get("Message")
                    let timeStamp = document.get("Time_Stamp")
                    let userEmail = document.get("User_Email")
                    let planDetails = document.get("planDetails")

                    // how to loop through planDetails
            }
        }

我已经走了这么远:

db.collection("Feedback_Message").getDocuments() { documentSnapshot, error in
            if let documentSnapshot = documentSnapshot {
                for document in documentSnapshot.documents {
                    guard let planDetails = document.get("planDetails") as? [[String : Any]] else {
                        let email = document.get("E-mail")
                        let message = document.get("Message")
                        let timeStamp = document.get("Time_Stamp")
                        let userEmail = document.get("User_Email")
                        let status = false
                    }
                    for plan in planDetails {
                        if let menuItemName = plan["menuItemName"] as? String {
                            // not sure I populate the menuItemsName, menuItemsQuantity and menuItemsPrice within the array
                        }
                    }

                }
            }
        }

更新2:

最终查询:终于可以正常使用了.但是无法填充正在扩展的单元格.

Final query: This is finally working as expected. But am not able to populate the expanding cells.

db.collection("Feedback_Message").getDocuments() { documentSnapshot, error in
            if let documentSnapshot = documentSnapshot {
                for document in documentSnapshot.documents {
                    guard let planDetails = document.get("planDetails") as? [[String : Any]],
                        let email = document.get("E-mail") as? String,
                        let message = document.get("Message") as? String,
                        let timeStamp = document.get("Time_Stamp") as? String,
                        let userEmail = document.get("User_Email") as? String,
                        let status = document.get("status") as? Bool else {
                            continue
                        }
                    for plan in planDetails {
                        guard let menuItemName = plan["menuItemName"] as? String,
                            let menuItemQuantity = plan["menuItemQuantity"] as? Int,
                            let menuItemPrice = plan["menuItemPrice"] as? Double else {
                                continue
                        }
                        let planDetails = PlanDetails(menuItemName: menuItemName, menuItemQuantity: menuItemQuantity, menuItemPrice: menuItemPrice)
                        let complaint = Complain(eMail: email, message: message, timeStamp: timeStamp, userEmail: userEmail, status: status, planDetails: planDetails)
                        self.messageArray.append(complaint)
                        print(self.messageArray)
                    }
                }
            }
        }

推荐答案

编辑

// OPTION 1 - FULL REQUIREMENT
db.collection("Feedback_Message").getDocuments() { documentSnapshot, error in
    if let documentSnapshot = documentSnapshot {
        for document in documentSnapshot.documents {
            // all of these properties are required to parse a single document
            guard let planDetails = document.get("planDetails") as? [[String : Any]],
                let email = document.get("E-mail") as? String,
                let message = document.get("Message") as? String,
                let timeStamp = document.get("Time_Stamp") as? String,
                let userEmail = document.get("User_Email") as? String,
                let status = document.get("status") as? Bool else {
                    continue // continue this loop
            }
            for plan in planDetails {
                // all of these properties are required to instantiate a struct
                guard let name = plan["menuItemName"] as? String,
                    let price = plan["menuItemName"] as? Double,
                    let quantity = plan["menuItemQuantity"] as? Int else {
                        continue // continue this loop
                }
                let plan = PlanDetails(menuItemName: name, menuItemQuantity: quantity, menuItemPrice: price)
                // then you will likely append this plan to an array 
            }
        }
    }
}

// OPTION 2 - PARTIAL REQUIREMENT
db.collection("Feedback_Message").getDocuments() { documentSnapshot, error in
    if let documentSnapshot = documentSnapshot {
        for document in documentSnapshot.documents {
            // all of these properties are required to parse a single document
            guard let planDetails = document.get("planDetails") as? [[String : Any]],
                let email = document.get("E-mail") as? String,
                let message = document.get("Message") as? String else {
                    continue // continue this loop
            }
            // these properties are not required
            let timeStamp = document.get("Time_Stamp") as? String // optional (can be nil)
            let userEmail = document.get("User_Email") as? String // optional (can be nil)
            let status = document.get("status") as? Bool ?? false // not optional because it has a default value of false
            for plan in planDetails {
                // all of these properties are required to instantiate a struct
                guard let name = plan["menuItemName"] as? String,
                    let price = plan["menuItemName"] as? Double,
                    let quantity = plan["menuItemQuantity"] as? Int else {
                        continue // continue this loop
                }
                let plan = PlanDetails(menuItemName: name, menuItemQuantity: quantity, menuItemPrice: price)
                // then you will likely append this plan to an array
            }
        }
    }
}

您可以通过多种其他方式来执行此操作,但这只是其中的几种.例如,您可以使用 [String:Any] 字典本身实例化该结构.

There are a number of other ways you can do this, these are just a couple. For example, you can instantiate the struct with the [String: Any] dictionary itself.

这篇关于如何遍历具有多个地图的Firestore文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:45