ios - 防止重新加载已报告的评论

标签 ios swift firebase firebase-realtime-database

我想防止已报告的评论被重新加载到我的评论表中。所以我做了一个removedIDs表:

 var removedIDs = [Comment]()

我也有

 var comments = [Comment] ()

然后我希望当您报告评论时,它会将其添加到列表中,这样:

  func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let more = UITableViewRowAction(style: .default, title: "Report") { action, index in self.comments.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade) }; more.backgroundColor = UIColor.blue; self.removedIDs.append(comments[indexPath.row]); return [more]           
}

然后我想在加载评论时我可以:

func loadComments(forPostId: String) {
    let ref = self.ref.child("comments")
    let thisPostRef = ref.child(forPostId)
    thisPostRef.observeSingleEvent(of: .value, with: { snapshot in

        let allComments = snapshot.children.allObjects as! [DataSnapshot]
        for commentSnap in allComments {
            let commenterUid = commentSnap.childSnapshot(forPath: "comment_by_uid").value as? String ?? "No uid"
            let commentText = commentSnap.childSnapshot(forPath: "comment_text").value as? String ?? "No comment"
          let aComment = Comment(id: commenterUid, text: commentText)
            if removedIDs.contains(aComment){
                print("skipped")
            }
            else {
                self.comments.append(aComment)
                print(commenterUid, commentText)
            }
        }
        self.commentsTable.reloadData()
    })
}

但我收到错误

Argument type 'Comment' does not conform to expected type 'Equatable'

上线:removedIDs.contain(aComment){

请注意,我的评论模型如下:

import Foundation

类评论{

var id:String
var text:String
init(id: String, text:String) {
    self.id = id
    self.text = text


}
static func parseComment(_ key:String, data:[String:Any]) -> Comment? {
    if let text = data["text"] as? String{
        return Comment(id: key, text: text)
    }


    return nil
    }
}

如何实现这个功能?

最佳答案

removedIDs.contain(aComment){需要Equatable一致性

  class Comment : Equatable {
    static func == (lhs: Comment, rhs: Comment) -> Bool {
        return lhs.id == rhs.id
    }
    var id:Int!
}

关于ios - 防止重新加载已报告的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185445/

相关文章:

ios - 与在 Interface Builder 中使用自动布局相比,SnapKit 有哪些优势?

ios - 使用 WKWebView 修改键盘工具栏/附件 View

ios - 属性(property)观察员 (KVO) : are they still the way to go?

local-storage - 使用 Firebase 进行 session 管理?

node.js - Firebase图像下载功能的Cloud Functions错误

iphone - ASIHttprequest 用于登录认证

ios - 44 像素的宽度是否等于所有设备的相同物理尺寸?

android - Phonegap iOS/Android - 链接 www 文件夹

swift - 如何在正确的坐标中快速将文本写入图像

firebase - 如何处理 Firebase 通知,即 Android 中的通知消息和数据消息