我刚刚创建,所以用户可以评论其他用户发布和删除评论,如果他们想删除它们 .

但使用此代码,每个人都可以删除每个人的评论

func getKeysValue() {

   // let uid = Auth.auth().currentUser?.uid

    Database.database().reference().child("posts").child(postsKey).child("comments").observe( .value) { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let commentKey = snap.key
            self.keyArray.insert(commentKey, at: 0)


         //   print(self.keyArray)
           // print("Here is the specific Key\(self.commentKey)")
        }
    }

}


   func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {



    if editingStyle == .delete {
       getKeysValue()

        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when, execute: {

          Database.database().reference().child("posts").child(postsKey).child("comments").child(self.keyArray[indexPath.row]).removeValue()


                        self.commentsTableView.reloadData()


                        })
                }
            }

然后我尝试这个代码只让用户只删除自己的评论:

func getKeysValue() {

   // let uid = Auth.auth().currentUser?.uid

    Database.database().reference().child("posts").child(postsKey).child("comments").observe( .value) { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let commentKey = snap.key
            self.keyArray.insert(commentKey, at: 0)


         //   print(self.keyArray)
           // print("Here is the specific Key\(self.commentKey)")
        }
    }

}


var uid = Auth.auth().currentUser?.uid
var userID = String()
var idUser = String()
var userUID = String()

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {



    if editingStyle == .delete {
       getKeysValue()

        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when, execute: {

            self.userID = self.keyArray[indexPath.row]

            self.idUser = self.userID
           print(" Here is the indexPath  key: \(self.idUser)")
            Database.database().reference().child("posts").child(postsKey).child("comments").child(self.idUser).observeSingleEvent(of: .value, with: { (snapshot) in


                if let postsDictionary = snapshot.value as? [String: AnyObject] {
                    self.uidArray.insert(postsDictionary as NSDictionary, at: 0)


                    let usersArray = self.uidArray[indexPath.row] as! [String: AnyObject]
                    let usersKey = usersArray["uid"] as? String
                    self.userUID = usersKey!


                            print("Here is the Comment:\(self.uidArray)")
                            print("And here is the UID::: \(self.userUID)")

                    if self.uid != self.userUID {

                        let logInAlert = UIAlertController(title: "Failed to delete", message: "You can only delete your own comment(s)", preferredStyle: .actionSheet)
                        logInAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                        self.present(logInAlert, animated: true, completion: nil)

                    }else {

                        let commentSuccess = self.storyboard?.instantiateViewController(withIdentifier: "CommentReloadVC")
                        self.present(commentSuccess!, animated: false, completion: nil)

                        Database.database().reference().child("posts").child(postsKey).child("comments").child(self.keyArray[indexPath.row]).removeValue()


                        self.commentsTableView.reloadData()

                                }
                            }
                        })
                    })
                }
            }

soo这个代码有什么问题吗?问题是这个工作完美,但只在tableview的第一行说,这个authorazation代码只适用于帖子的第一个评论,如果你试图删除第一个下面的任何其他评论,那么我得到一个错误索引超出此行的范围

让usersArray = self.uidArray [indexPath.row]为! [String:AnyObject]

这就是它在数据库中的样子:
enter image description here

这看起来应该可以工作,但它不是它可能真的很容易,但我只是无法弄清楚这样的帮助将非常感谢

谢谢你的时间 . :)