如何在Swift中获取Cloud Firestore对象

问题描述

Firestore database setup

因此,我仍然有点想检索我的云Firestore数据库中的嵌套对象。我认为我的结构正确,但是当我实际尝试打印时,没有任何显示。现在我也没有错误。模拟器运行正常,但也不会打印任何内容。所以我知道出了点问题,但我无法查明。以下是我如何构造数据模型以及如何输出或尝试构建数据模型。

protocol DocumentSerializable  {
    init?(dictionary:[String:Any])
}

struct Itemmodel{
   var merchant : Merchant
   
    var dictionary: [String: Any] {
        return [
            "merchant": merchant
        ]
    }
}

extension Itemmodel: DocumentSerializable {
    
    init?(dictionary: [String : Any]) {
        guard let merchant = dictionary["merchant"] as? Merchant
            else { return nil }
        
        
        self.init( merchant: merchant)
    }
}


struct Merchant {
    var first: String
    var last: String

    
    var dictionary:[String:Any] {
        return [
            "first": first,"last": last
        ]
    }
}

extension Merchant : DocumentSerializable {
    init?(dictionary: [String : Any]) {
        guard
            let first = dictionary["first"] as? String,let last = dictionary["last"] as? String
        else { return nil }
        self.init(first:first,last:last)
        
    }
}

这是我的输出块。

var receiptList = [Itemmodel]()

func loadData() {
    db.collection("test").getDocuments() {
        querySnapshot,error in
        if let error = error {
            print("\(error.localizedDescription)")
        }else{
            self.receiptList = querySnapshot!.documents.compactMap({Itemmodel(dictionary: $0.data())})
            dispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
    
    
}

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "purchaseblock",for: indexPath)
    
 
    let Itemmodel = receiptList[indexPath.row]
    
    cell.textLabel?.text = " \(Itemmodel.merchant )"
    //cell.detailTextLabel?.text = "10.99"
    
    return cell
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)