Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift和ios 14中Firebase Firestore文档的访问字段?_Ios_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Swift和ios 14中Firebase Firestore文档的访问字段?

Swift和ios 14中Firebase Firestore文档的访问字段?,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,首先,我对firebase非常陌生,所以这可能是一个很容易回答的问题。我正在使用firebase上的firestore将用户名和全名保存在以用户电子邮件为名称的文档下,并保存在名为users的集合中。在ios 14中,没有AppDelegate页面,因此我一直将有关firebase的内容放在下面: class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplicati

首先,我对firebase非常陌生,所以这可能是一个很容易回答的问题。我正在使用firebase上的firestore将用户名和全名保存在以用户电子邮件为名称的文档下,并保存在名为
users
的集合中。在ios 14中,没有
AppDelegate
页面,因此我一直将有关firebase的内容放在下面:

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions
                        launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
我看到以下示例代码以获取文档:

let docRef = db.collection("cities").document("SF")

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

但在此之后,我不知道如何访问用户名和全名。甚至如何正确存储此文档?我在这里做什么?

要从检索到的文档中获取字段值,可以执行以下操作:

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
        
        let data = document.data()
        let username = data!["username"]! as? String ?? ""
        let fieldName = data!["fieldName"]! as? String ?? ""
        print(username)
    } else {
        print("Document does not exist")
    }
}