如何使用Firestore做到这一点?

时间:2019-08-14 05:50:31

标签: swift database google-cloud-firestore

我正在使用Firestore存储用户的名称,并且试图访问存储的名称,但是我不知道使用哪种方法来获得与此代码和实时数据库配对的结果相似的结果。

if Auth.auth().currentUser?.uid != nil {
               let uid = Auth.auth().currentUser?.uid
                Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
                       print("snapshot", snapshot)
                    let value = snapshot.value as? NSDictionary
                    let dictionary = value as? [String: AnyObject]
                        print("dictionary", dictionary)          // To test if nil
                        print("first name", dictionary?["first name"])
                    // test if nil ; could be if key is not correct (uppercase?)
                    self.userNameLabel.text = dictionary?["first name"] as? String ?? "Didn't work"
                    }, withCancel: nil)
            }
}

0 个答案:

没有答案