Home > Blockchain >  Remove Message with MessageID From MessageList
Remove Message with MessageID From MessageList

Time:08-10

I have messageList which has all the messages in the chat. I want to remove only one message with the specific messageId but .remove only asks for index as Int. How can I remove that message from the messageList?

func deleteMessage(_ message: ChatMessageViewModel) {
        let messageID = message.messageId
        messageList.remove(object: messageID)
        messagesCollectionView.reloadData()
    }

enter image description here

CodePudding user response:

Try below code:

func deleteMessage(_ message: ChatMessageViewModel) {
    let messageID = message.messageId
    messageList.removeAll(where: { $0.messageId == messageID })
    messagesCollectionView.reloadData()
}
  • Related