swift 如何修复类型“ChatViewController”不符合协议“MessagesDataSource”

x33g5p2x  于 2023-02-28  发布在  Swift
关注(0)|答案(1)|浏览(205)

我得到一个错误:* 类型'ChatViewController'不符合协议'MessagesDataSource'*当运行此swiftui代码,但我不知道我错过了什么.

extension ChatViewController: MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {

    func currentSender() -> SenderType {
        if let sender = selfSender {
            return sender
        }
        
        fatalError("Self Sender is nil, email should be cached")
    }
    
    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messages[indexPath.section]
    }
    
    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }
    
    func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
        guard let message = message as? Message else {
            return
        }
        
        switch message.kind {
        case .photo(let media):
            guard let imageUrl = media.url else {
                return
            }
            
            imageView.sd_setImage(with: imageUrl)
            
        default:
            break
        }
    }
    
    func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
        let sender = message.sender
        if sender.senderId == selfSender?.senderId {
            // our message thar we've sent
            return .link
        }
        
        return .secondarySystemBackground
    }
    
    func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
        let sender = message.sender
        if sender.senderId == selfSender?.senderId {
            // show our profile image
            if let currentUserImageUrl = self.senderPhotoUrl {
                avatarView.sd_setImage(with: currentUserImageUrl)
            } else {
                //images/safeemail_profile_picture.png
                guard let email = UserDefaults.standard.value(forKey: Keys.email.rawValue) as? String else {
                    return
                }
                
                let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
                let path = "images/\(safeEmail)_profile_picture.png"
                
                //fetch url
                StorageManager.shared.downloadURL(for: path) { [weak self] result in
                    switch result {
                    case .success(let url):
                        DispatchQueue.main.async {
                            self?.senderPhotoUrl = url
                            avatarView.sd_setImage(with: url)
                        }
                    case .failure(let error):
                        print("Failed to download image url: \(error)")
                    }
                }
            }
        } else {
            // show other user profile image
            if let otherUserImageUrl = self.otherUserPhotoUrl {
                avatarView.sd_setImage(with: otherUserImageUrl)
            } else {
                //fetch url
                let email = self.otherUserEmail
                
                let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
                let path = "images/\(safeEmail)_profile_picture.png"
                
                //fetch url
                StorageManager.shared.downloadURL(for: path) { [weak self] result in
                    switch result {
                    case .success(let url):
                        DispatchQueue.main.async {
                            self?.otherUserPhotoUrl = url
                            avatarView.sd_setImage(with: url)
                        }
                    case .failure(let error):
                        print("Failed to download image url: \(error)")
                    }
                }
            }
        }
    }
}

我尝试了xcode的自动修复:* 是否要添加协议存根?* 执行以下操作:

extension ChatViewController: MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {
    var currentSender: MessageKit.SenderType {
        <#code#>
    }

但是我又得到了一个新的错误:* "currentSender()"的重新声明无效 *

9nvpjoqh

9nvpjoqh1#

MessagesDataSource协议将var currentSender: SenderType { get }作为其要求之一。
这意味着必须将currentSender实现为varlet属性,而不是func

相关问题