So I am trying to create a function where users can delete their own comment/report others.
I've done this with the post so I assumed I would use the same method..which is grabbing the owner of the comments UID and check if that is the current user or not. If it is the current user then I will set up the alert controller to display "delete" else it'll be "report"
Here is a sample code of the post function that works correctly
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()
Also the post value in FollowingCell
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()
}
}
Here is the code I have for comments
@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")
}
}
Also here is the code I have for the 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()
}
}
}
When I run the program I get a crash
Thread 1: EXC_BAD_ACCESS (code=257, address=0x1a2494098a1)
on the line guard let comment = cell.comment else { return }
I ran a breakpoint and everything is coming out to nil ( the text of the comment, the user's information who posted the comment, everything) Does anyone know how I can fix this? I am also using Active Label so I'm not sure if that is playing a factor not. Thanks!
Well, after 6 days of trying to figure out what I did wrong.. It appears all I forgot to add was
cell.delegate = self
under cellForItemAt
on the collection view function. So if anyone has this issue I hope this helps!
I love coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With