展开一个可选值(firebase,swift)时意外发现nil

时间:2018-10-12 08:30:58

标签: swift firebase google-cloud-firestore

我想从firebase获取teamID,但是会发生上述错误。 这是用户首次注册(注册⇨登录集)时发生的情况。 第二次或以后登录时,您可以从firebase获取团队ID。 我不知道为什么在第一次注册时会发生这种情况。 你能告诉我如何解决吗?

override func viewDidLoad() {
        super.viewDidLoad()

        circleButton = Circle()

        if arr != nil {
            self.arr = []
            self.dataNameFromFireStore = [Any]()
            self.dataTimeFromFirestore = [Any]()
            self.dataTitleFromFireStore = [Any]()
            self.submitDocumentIdArr = [Any]()

            self.selectedNum = 1

            userTable.delegate = self
            userTable.dataSource = self
            userTable.register(UINib(nibName: "userTableViewCell", bundle: nil), forCellReuseIdentifier: "cellName")

            self.db.collection("users").document(self.fireAuthUID).addSnapshotListener { (snapshot3, error) in
                guard let document3 = snapshot3 else {
                    print("erorr2 \(String(describing: error))")
                    return
                }
                let data = document3.data()

                self.teamIDFromFirebase = (data!["teamID"] as? String)! ← An error occurred here!!!!!!!!!!

                self.db.collection("diary").document(self.teamIDFromFirebase).collection("diaries").whereField("submit", isEqualTo: true).getDocuments() { (querySnapshot, err) in
                    if let err = err {
                        print("Error getting documents: \(err)")
                    } else {
                        var i = 0
                        for document in querySnapshot!.documents {
                            self.timelineDocumentIdArr.append(document.documentID)

                            let documentData = document.data()
                            self.dataTitleFromFireStore.append((documentData["今日のタイトル"] as? String)!)
                            self.dataTimeFromFirestore.append((documentData["time"] as? String)!)
                            self.dataNameFromFireStore.append((documentData["userName"] as? String)!)
                            self.arr.append(CellData(image: UIImage(named: "weight")!, name: self.dataNameFromFireStore[i] as! String, time: self.dataTimeFromFirestore[i] as! String, title: self.dataTitleFromFireStore[i] as! String))
                            print(self.arr)

                            i += 1

                        }
                        self.userTable.reloadData()
                        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                        print(self.timelineDocumentIdArr)
                    }
                }
            }
        } else {
            return arr = [CellData(image: UIImage(named: "")!, name: "", time: "", title: "")]
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是,您正在强制拆开document3以及querySnapshot中的数据。因此,当没有数据时,您的应用程序将崩溃...请尝试以下代码,让我知道它是否有效!

override func viewDidLoad() {
    super.viewDidLoad()

    circleButton = Circle()

    if arr != nil {
        self.arr = []
        self.dataNameFromFireStore = [Any]()
        self.dataTimeFromFirestore = [Any]()
        self.dataTitleFromFireStore = [Any]()
        self.submitDocumentIdArr = [Any]()

        self.selectedNum = 1

        userTable.delegate = self
        userTable.dataSource = self
        userTable.register(UINib(nibName: "userTableViewCell", bundle: nil), forCellReuseIdentifier: "cellName")

        self.db.collection("users").document(self.fireAuthUID).addSnapshotListener { (snapshot3, error) in
            guard let document3 = snapshot3 else {
                print("erorr2 \(String(describing: error))")
                return
            }

            guard let data = document3.data() else { return }
            self.teamIDFromFirebase = data["teamID"] as? String ?? ""

            self.db.collection("diary").document(self.teamIDFromFirebase).collection("diaries").whereField("submit", isEqualTo: true).getDocuments() { (querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    var i = 0
                    guard let querySnapshot = querySnapshot else { return }
                    for document in querySnapshot.documents {
                        self.timelineDocumentIdArr.append(document.documentID)

                        let documentData = document.data()
                        self.dataTitleFromFireStore.append(documentData["今日のタイトル"] as? String ?? "")
                        self.dataTimeFromFirestore.append(documentData["time"] as? String ?? "")
                        self.dataNameFromFireStore.append(documentData["userName"] as? String ?? "")
                        self.arr.append(CellData(image: UIImage(named: "weight")!, name: self.dataNameFromFireStore[i] as? String ?? "", time: self.dataTimeFromFirestore[i] as? String ?? "", title: self.dataTitleFromFireStore[i] as? String ?? ""))
                        print(self.arr)

                        i += 1

                    }
                    self.userTable.reloadData()
                    print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                    print(self.timelineDocumentIdArr)
                }
            }
        }
    } else {
        return arr = [CellData(image: UIImage(named: "")!, name: "", time: "", title: "")]
    }
}