Home > other >  Delete Message From MessageList Swift
Delete Message From MessageList Swift

Time:08-05

I am trying to delete a message from the message list.

When tapped on the message I want to remove it with

func deleteMessage(_ message: ChatMessageViewModel) {
        messageList = messageList.remove(object: message)
        interactor?.updateMessagesModel(message: message.messageModel)
    }

But I got an error

Cannot assign value of type '()' to type '[ChatMessageViewModel]'

This is the view model

final class ChatMessageViewModel: MessageType, Equatable {
    static func == (lhs: ChatMessageViewModel, rhs: ChatMessageViewModel) -> Bool {
        return lhs.messageId == lhs.messageId
    }
    
    var messageModel: ChatReceivedMessageModel!
    
    var sender: SenderType {
        return mockUser
    }
    var messageId: String
    var sentDate: Date
    var kind: MessageKind
    var mockUser: MockUser
    var room: String?
    var message: String?
    var userToConnect: String?
    var userFullName: String?
    var senderId: String?
    var senderPhotoUrl: String?
    var creationTimestamp: Int64?
    var prevTimeStamp: Int64?
    
    init(messageModel: ChatReceivedMessageModel) {
        self.messageModel = messageModel
        self.room = messageModel.room
        self.message = messageModel.message
        self.userToConnect = messageModel.userToConnect
        self.userFullName = messageModel.userFullName
        self.senderId = messageModel.senderId
        self.senderPhotoUrl = messageModel.senderPhotoUrl
        self.creationTimestamp = messageModel.creationTimestamp
        self.messageId = messageModel.messageId ?? ""
        self.kind = .text(messageModel.message ?? "")
        var displayName = ""
        if Login.sharedInstance.user.id.uppercased() == messageModel.senderId {
            displayName = "\(Login.sharedInstance.user.name ?? "") \(Login.sharedInstance.user.lastName ?? "")"
        }
        self.mockUser = MockUser(senderId: messageModel.senderId ?? "", displayName: displayName)
        self.sentDate = Date(milliseconds: messageModel.creationTimestamp ?? 0)
        self.prevTimeStamp = 0
    }
    
}

CodePudding user response:

Problem is here

messageList = messageList.remove(object: message)

remove returns void , so you only need

messageList.remove(object: message)
  • Related