从Cloud Firestore中的地图获取数据

时间:2019-05-11 13:26:28

标签: swift google-cloud-firestore

我想从我的Firestore数据结构中获取标题和正文,如下图所示。我尝试了这段代码,获得了完整的数据,但是我想分别显示标题和正文。

        let docRef = db.collection("test").document("test")

    docRef.getDocument { (document, error) in
        if let notidata = document, notidata.exists {
            let notiDetail = notidata.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(notiDetail)")
        } else {
            print("Document does not exist")
        }
    }

请帮助我。

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试此代码:-

docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            print(document.data()!)
            let userNotifications = document.data()["userNotifications"] as? [[String:Any]]
            for notificaton in userNotifications  {
            let body = notificaton["body"] as? String ?? ""
            let title = notificaton["title"] as? String ?? ""
            print(body, title)
         }

        } else {
            print("Document does not exist")
        }
    }