Ios 找不到值时出错,但我在前面使用了类似的方法来检索文档

Ios 找不到值时出错,但我在前面使用了类似的方法来检索文档,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,我正在处理PostCommentFeed,其中有一个CommentButton,该按钮将指向与特定帖子相关的评论列表。但当我试图实现这个方法时,它说的是无效的可选值,我在前面使用了类似的方法来获取注释。但现在它不起作用了,请告诉我哪里出了问题 class HomeViewController: UITableViewController { var posts = [Post]() var db: Firestore! var postKey:String = ""

我正在处理PostCommentFeed,其中有一个CommentButton,该按钮将指向与特定帖子相关的评论列表。但当我试图实现这个方法时,它说的是无效的可选值,我在前面使用了类似的方法来获取注释。但现在它不起作用了,请告诉我哪里出了问题

class HomeViewController: UITableViewController {
    var posts = [Post]()
    var db: Firestore!
    var postKey:String = ""
    private var documents: [DocumentSnapshot] = []
    private var listener : ListenerRegistration!
    var postId1:String = ""
    var detailView: Post!
    var onlyforPostKey: Post?

    override func viewDidLoad() {
        super.viewDidLoad()
        db = Firestore.firestore()
        retrieveAllPosts()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func handleLogout(_ sender:Any) {
        try! Auth.auth().signOut()
        self.dismiss(animated: false, completion: nil)
    }

    func retrieveAllPosts(){
        let postsRef = Firestore.firestore().collection("posts").limit(to: 50)
        postsRef.getDocuments { (snapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                if let snapshot = snapshot {
                    for document in snapshot.documents {
                        let data = document.data()
                        let username = data["post_author_username"] as? String ?? ""
                        let postTitle = data["postTitle"] as? String ?? ""
                        let postcategory = data["postcategory"] as? String ?? ""
                        let postContent = data["postContent"] as? String ?? ""
                        let postAuthorProfilePicUrl = data["post_user_profile_pic_url"] as? String ?? ""
                        let postAuthorSpinnerC = data["post_author_spinnerC"] as? String
                        let newSourse = Post(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
                        self.posts.append(newSourse)
                        print(self.postKey)
                    }
                    self.tableView.reloadData()
                }
            }
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //tableView.reloadData()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
        cell.btnComment.tag = indexPath.row
        cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
        cell.set(post: posts[indexPath.row])
        return cell
    }

    @objc func toComments(_ sender: AnyObject) {
        let buttonPosition = sender.convert(CGPoint.zero, to: tableView)
        let indexPath: IndexPath? =  tableView.indexPathForRow(at: buttonPosition)
        detailView = posts[(indexPath?.row)!]
        performSegue(withIdentifier: "toCommentsList", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        postKey = onlyforPostKey!._documentId
        var vc = segue.destination as! CommentListViewController
        vc.postId = postKey
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let post = self.posts[indexPath.row]
    }
}

“它说的是无效的可选值”Where?@Larme我在发布问题时遇到了“在隐式展开可选值时意外发现零”的错误,请提供详细信息-哪一行崩溃了?您是否仔细检查了代码以查看什么var是nil?另外,在这一行中,让postAuthorSpinnerC=data[“post\u author\u spinnerC”]as?字符串没有零合并,所以这可能是罪魁祸首。@Jay这个问题现在已经由我自己解决了,如果还需要什么,我会让你知道的