Home>

I am using chatkit to create a chat, but an error appears in one sentence of extension ChatViewController: MessagesDataSource. I think I have implemented all the required methods to resolve the error, but it doesn't. I would like to tell you what went wrong.

import UIKit
import MessageKit
public protocol MessageType {
    var sender: Sender {get}
    var messageId: String {get}
    var sentDate: Date {get}
    var kind: MessageKind {get}
}
public struct SenderType {
    public let id: String
    public let displayName: String
}

class ChatViewController: MessagesViewController {

        override func viewDidLoad () {
            super.viewDidLoad ()
            messagesCollectionView.messagesDataSource = self
            messagesCollectionView.messagesLayoutDelegate = self
            messagesCollectionView.messagesDisplayDelegate = self
}
}

let sender = SenderType (id: "any_unique_id", displayName: "Steven")
let messages: [MessageType] = []

extension ChatViewController: MessagesDataSource {
// Here you see Type 'ChatViewController' does not conform to protocol 'MessagesDataSource'

    func currentSender ()->SenderType {
        return SenderType (id: "any_unique_id", displayName: "Steven")
    }
    func numberOfSections (in messagesCollectionView: MessagesCollectionView)->Int {
        return messages.count
    }
    func messageForItem (at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView)->MessageType {
        return messages [indexPath.section]
    }
}
extension ChatViewController: MessagesDisplayDelegate, MessagesLayoutDelegate {}
  • Answer # 1

    As advised last time, the MessageType protocol and SenderType protocol are not defined by yourself

    public protocol MessageType {
        var sender: Sender {get}
        var messageId: String {get}
        var sentDate: Date {get}
        var kind: MessageKind {get}
    }
    public struct SenderType {
        public let id: String
        public let displayName: String
    }

    must be deleted.

    And as a structure conforming to the SenderType protocol
    For example, create a User structure

    public struct User: SenderType {
        public let senderId: String
        public let displayName: String
    }

    SenderType (id: "any_unique_id", displayName: "Steven")
    Not
    User (senderId: "any_unique_id", displayName: "Steven")

    If

    , the error will disappear for the time being.