【发布时间】:2020-01-03 03:51:09
【问题描述】:
所以我正在尝试创建一个功能,用户可以在其中删除自己的评论/举报他人。
我已经在帖子中做到了这一点,所以我假设我会使用相同的方法......它正在获取 cmets UID 的所有者并检查它是否是当前用户。如果是当前用户,那么我将设置警报控制器以显示“删除”,否则它将是“报告”
这是一个可以正常工作的 post 函数的示例代码
func handleOptionsTapped(for cell: FollowingCell) {
guard let post = cell.post else { return }
// If post belongs to current user display delete action sheet.. else report
if post.ownerUid == Auth.auth().currentUser?.uid {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Delete Post", style: .destructive, handler: { (_) in
post.deletePost()
还有FollowingCell中的post值
var post: Post? {
didSet {
guard let ownerUid = post?.ownerUid else { return }
guard let imageUrl = post?.imageUrl else { return }
Database.fetchUserWithUID(with: ownerUid) { (user) in
self.profileImage.loadImage(with: user.profileImageUrl)
self.username.setTitle(user.username, for: .normal)
self.configurePostCaption(user: user)
}
postImage.loadImage(with: imageUrl)
configureLikeButton()
}
}
这是我的 cmets 代码
@objc func handleCommentTapped(for cell: CommentCell) {
guard let comment = cell.comment else { return }
// If comment belongs to the current user
if comment.uid == Auth.auth().currentUser?.uid {
print("this is my comment")
} else {
print("this is another users comment")
}
}
这也是我为CommentCell 提供的代码
var comment: Comment? {
didSet {
guard let comment = self.comment else { return }
guard let uid = comment.uid else { return }
guard let user = self.comment?.user else { return }
guard let profileImageUrl = user.profileImageUrl else { return }
Database.fetchUserWithUID(with: uid) { (user) in
self.profileImageView.loadImage(with: profileImageUrl)
self.configureCommentLabel()
}
}
}
当我运行程序时,我遇到了崩溃
线程 1:EXC_BAD_ACCESS(代码=257,地址=0x1a2494098a1)
上线guard let comment = cell.comment else { return }
我跑了一个断点,一切都为零(评论的文本,发布评论的用户信息,一切)有谁知道我该如何解决这个问题?我也在使用 Active Label,所以我不确定这是否是一个因素。谢谢!
【问题讨论】:
-
感谢道格的编辑!老实说,我不太确定要使用哪些标签。
-
如前所述,我写了这篇文章作为题外话评论,所以不,我没有任何代码可以分享。我只是不明白为什么你在不使用数据库时从数据库中获取用户,而是使用评论中的用户和 url(第 3 和第 4 个保护语句)
-
你在哪里打电话给
handleCommentTapped?您能否发布调用此方法的代码,或提及它如何链接/绑定到评论单元格上的“点击”? -
我将编辑这个问题,并包括更多关于从什么被调用的细节:)
-
由于某种原因,您无法访问函数中的单元格。刚刚被点击的单元格不太可能被释放。更有可能的是,调用您的函数的实体对单元格的引用很弱,并且在控制到达您的代码时该引用无效。简单地将输入
cell参数设为可选 (CommentCell?) 并防范cell?.comment,很可能会避免崩溃,但不会使您的代码按预期工作。尝试将其作为第一步,以继续调试它而不会崩溃。
标签: ios swift firebase firebase-authentication