微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Swift中获取Cloud Firestore对象

如何解决如何在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 举报,一经查实,本站将立刻删除。