类型“ ChatViewController”不符合协议“ MessagesDataSource”

时间:2019-02-28 03:55:36

标签: swift messagekit

我正在学习Swift,并试图启动和运行MessageKit。我按照入门指南进行操作,但始终出现错误:类型“ ChatViewController”不符合协议“ MessagesDataSource”

这是我的代码:

import UIKit
import MessageKit

class ChatViewController: MessagesViewController {

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

override func viewDidLoad() {
    super.viewDidLoad()
    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messagesCollectionView.messagesDisplayDelegate = self
  }
}
extension ChatViewController: MessagesDataSource {

func currentSender() -> Sender {
    return Sender(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 {}

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

                       $.ajax({
                                method: 'POST',
                                url: MyAPILink,
                                data: JSON.stringify(arg),
                                dataType: 'json',
                                contentType: 'application/json',
                            }).done(function (resp){
                                // console.log(resp)
                                if(resp.data != null){
                                    var nameArray = [];
                                    $.each(resp.data,function(key,val){
                                        nameArray.push({
                                            idNumber: val.id,
                                            supplierName: val.supplier,
                                            DueDate: val.limit_payment_date,
                                            status: val.status,
                                            is_paid: val.is_paid,
                                            amount: val.amount
                                        });
                                    });
                                    nameArray.sort(function (a, b) {
                                        return b.id - a.id
                                    });
                                    option.success({
                                        data: nameArray,
                                        total: resp.total
                                    })
                                }

                            }).fail(function(jqXHR, status, err){
                                option.error(err);
                            });

答案 1 :(得分:0)

您错过了senderType协议,因此请尝试

struct sender: SenderType{
    var senderId: String
    var displayName: String
}